Mac OSX에서 Python을 사용하는 Arduino 📝

아두이노와 파이썬



이 소개에서는 다른 하드웨어 부품이나 LED를 연결하지 않고 이전 버전Arduino Nano으로 작업했습니다. 이 단계별 설명 뒤에 있는 아이디어는 약간의 연습을 하고 제어 방법을 찾는 것이었습니다 Arduino는 Arduino IDE 소프트웨어와 직접적으로 연결되지 않으며 C/C++ 표준 라이브러리 언어의 하위 집합이지만 Python 스크립트로 제어합니다. 실제로 구현하려면 위에서 언급한 것들이 필요하지만 부분적으로만 필요합니다.

간단히 말해서 Python 스크립트를 사용하여 Arduino LED에 매우 간단한 신호를 보낼 것이라고 말했습니다. LED를 켜거나 끄는 신호입니다.



이 튜토리얼은 다른 Linux 기반 시스템이나 Windows에도 적용할 수 있습니다.

공식 Arduino IDE 설치



공식Arduino website에서 Mac OS X용 ZIP 파일을 간단히 다운로드해 보겠습니다. C/C++가 Arduino Nano와 통신할 수 있도록 일종의 Python 라이브러리를 장치에 업로드하는 데 사용할 것입니다. 파일의 압축을 풀고 Arduino.appApplications 디렉토리에 드롭하여 정리된 상태로 유지합니다. 이 앱을 처음으로 시작하면 바로 팝업되는 짧은 스크립트 예시와 함께 첫 번째 창을 닫을 수 있습니다. 아래 스크린샷에서 볼 수 있듯이 File > Examples > Firmata > StandardFirmata로 이동합니다.



이 스크립트는 다음 창에 표시됩니다.



Arduino 연결 및 스크립트 업로드



이 단계 전에 Arduino를 컴퓨터나 노트북에 연결해야 합니다. 한쪽 끝에는 Mini B 5핀으로 알려진 Mini USB가 필요하고 다른 쪽 끝에는 USB 2 A 케이블이 있어야 합니다. 테스트에 사용하는 MacBook 모델에 따라 이 끝점에 대한 약간의 감소를 찾을 수 있습니다.

이제 창 맨 위에 있는 업로드 버튼(오른쪽을 가리키는 화살표)을 누르기만 하면 됩니다.



창 하단에 "업로드 완료"라는 정보가 표시됩니다. 그리고 하단 콘솔 출력에서 ​​이와 같이.



Firmata 스크립트가 Arduino Nano에 업로드되었으며 이제 전체Arduino.app를 닫을 수 있습니다. 전혀 작업할 필요가 없습니다.

Arduino와의 통신을 위한 Python 라이브러리



나는이 모든 것을 pyFirmata로 실행하려고 노력했지만 Arduino 모델을 감지하기 위해 내 Python 스크립트에 문제가 있었기 때문에 pyFirmata2에 대한 후속 조치가 있었고 잘 작동했습니다. 빠르고 원활하게 설치하려면 이 코드를 콘솔에 입력하십시오.

pip3 install pyfirmata2


Pip3 설치에 관한 아래 참고 사항



위의 Pip3 설치가 제대로 작동하지 않는 경우 주된 이유는 Pip3Python를 전혀 설치하지 않았기 때문입니다. brew.sh을 방문하여 웹 사이트에 나열된 코드를 복사하여 붙여넣으십시오.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"


그런 다음 brew install python3 로 python3을 설치한 후.

brew install python3


그리고 지금은 pip3 install pyfirmata2를 설치하여 pyfirmata2를 시도하십시오.

Arduino를 깜박이는 Python 스크립트



Arduino로 게임을 시작하고 깜박이는 마술을 보려면 아래 스크립트에 대한 링크를 방문하고 예를 들어 데스크톱에 저장하려면 CTRL + S를 누르십시오. 이 스크립트는 보드에 L로 레이블이 지정된 LED를 켭니다(스크립트에서는 board.digital[13] 코드로 이 LED를 호출합니다.

👉 Download Python Blink Script

또는 전체 스크립트도 여기에서 찾을 수 있습니다.

from pyfirmata2 import Arduino #import library from pyfirmata2 to detect Arduino
import time #time library to be able setup lenght of led lighting

board = Arduino(Arduino.AUTODETECT) #detect Arduino with Autodetect

print("...Arduino detected") #print statement #1
print("...Blink test started") #print statement #2

while True: #while this statement is true execute script hereunder
    print("...Blink LED 1st time") #print statement blink 1st time
    board.digital[13].write(1)
    time.sleep(2) #light up led number 13 for 2 seconds
    board.digital[13].write(0)
    time.sleep(1) #light off led number 13 for 1 seconds
    print("...Blink LED 2nd time") #print statement blink 2nd time
    board.digital[13].write(1)
    time.sleep(2) #light up led number 13 for 2 seconds
    board.digital[13].write(0)
    time.sleep(1) #light off led number 13 for 1 seconds
    print("...Blink LED 3rd time") #print statement blink 3rd time
    board.digital[13].write(1)
    time.sleep(2) #light up led number 13 for 2 seconds
    board.digital[13].write(0)
    time.sleep(1) #light off led number 13 for 1 seconds
    print("...Blink LED 4th time") #print statement blink 4th time
    board.digital[13].write(1)
    time.sleep(2) #light up led number 13 for 2 seconds
    board.digital[13].write(0)
    time.sleep(1) #light off led number 13 for 1 seconds
    print("...Blink test successfull!") #print statement #3
    exit() #exit script


이 스크립트를 실행하려면 데스크탑으로 이동하여 아래의 명령을 사용하여 터미널에서 실행하십시오.

cd Desktop
python3 blink-arduino.py


이와 같은 터미널에서 상태 정보를 볼 수 있습니다.

...Arduino detected
...Blink test started


이제 LED가 2초 동안 4번 켜지고 1초 동안 꺼지면 터미널에서 꺼질 때마다 이와 같은 상태 정보가 표시되어야 합니다.

...Blink LED 1st time
...Blink LED 2nd time
...Blink LED 3rd time
...Blink LED 4th time


이 작은 깜박임 의식이 끝나면 이 정보를 볼 수 있습니다.

...Blink test successfull!


마지막 말



LED 조명의 빈도와 길이를 변경하는 스크립트를 가지고 자유롭게 놀아보세요. 제가 가지고 노는 이 작은 스크립트 연습으로 거의 모든 작업이 완료되었습니다.

Unsplash의 표지 이미지를 제공해 주신 Mathew Schwartz에게 감사드립니다.

좋은 웹페이지 즐겨찾기