We can directly use C++ on Arduino instead of using Arduino’s default way. Also, we can code with C on Arduino. As readers possibly know – Arduino does not have any special language. But, How to Use Python on Arduino, For Tasks Like to Blink LED?. We need not to compile the Python scripts unlike C++ but simply run the Python scripts from Terminal/iTerm2. That is not exactly great, but kind of Python with help of some computer. Which is great when Arduino is combined with Raspberry Pi and script needs changing faster.
How to Use Python on Arduino, Like to Blink LED?
Open Terminal/iTerm2 and install pyserial
and pyserial
for Python-Arduino interaction:
1 2 | pip install pyserial pip install pyfirmata |
Now open Arduino IDE, connect Arduino board. Go to default example sketches, hover over examples of Firmata. Open StandardFirmata
. Upload that StandardFirmata sketch on Arduino. Firmata is a protocol. You’ll get more information here :
---
1 2 3 | https://www.arduino.cc/en/Reference/Firmata http://firmata.org/wiki/Main_Page https://github.com/firmata/arduino |
Download that “Firmata Test Program”. Run that program. Select port from dropdown where Arduino is, on MacBook Pro, it was /dev/cu.usbmodem1411
for me. For GNU/Linux, it will be like /dev/tty.usbmodem1411
. For Windows, it will be like COM 3
. Test by clicking Low beside Pin 13.
I have a Python script for Mac, Linux. Be careful to change the port for your system after really checking. Save it as something like blink.py
and run with command python blink.py
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import pyfirmata import time pin= 13 port = '/dev/cu.usbmodem1411' board = pyfirmata.Arduino(port) while True: board.digital[pin].write(1) time.sleep(0.9) # delays for 9 seconds board.digital[pin].write(0) time.sleep(0.9) # delays for 9 seconds board.exit() |
The LED on Arduino board should blink. RX will “wink” for a while. That worked for me. At this moment I am not expert around running Python scripts on Arduino. But logically the above way should make other things working.
There is an old discussion which can help you :
1 | http://forum.arduino.cc/index.php?topic=225329.0 |