Python has become de-facto programming language for AI and Machine Learning domains. For kids to teach python with solving these machine learning problems is a difficult task, because these domains are so complex to understand for primary school kids. Kids enjoy programming through Arduino kind of computing platforms where they can interact with physical devices like LEDs, Motors etc..
As the RAM, processing power of Arduino doesn’t support running python interpreted code on Arduino, there is a python library pyfirmata developed to write python code for Arduino.
Pyfirmata is based on firmata protocol. Details of the firmata can be found at https://github.com/firmata/protocol
Pyfirmata uses pyserial library to communicate with Arduino over UART. To enable Arduino to respond to pyfirmata calls Arduino should be loaded with firmata firmware. This is a part of Arduino examples, below figure shows how to load firmata.
Once the firmata firmware is loaded, now the Arduino is ready to accept commands from pyfirmata.
install the pyfirmata on the host computer using
bash# pip install pyfirmata.
from pyfirmata import Arduino, util
import time
board = Arduino('COM4')
board.digital[13].write(1)
while True:
board.digital[13].write(1)
time.sleep(1)
board.digital[13].write(0)
time.sleep(1)
Above code snipet shows we have to get the board instance from Arduino(‘COM4’), here COM4 is the serial port to which Arduino is connected.
board.digital[13].write(1) using this line we are making GPIO 13 of Arduino logic HIGH.
time.sleep(1) This is regular python time functions for delay of 1 second.
board.digital[13].write(0) using this line we are making GPIO 13 of Arduino logic LOW.
This loop is LED blinky program of Arduino using pyfirmata.