Can you use Python programming language to program an Arduino? Well, yes and no. I will show you how I work together for Python programming language and theArduino platform.
Why Python with Arduino?The main idea here is to run state of the art AI program on thelaptop as the main processing unit and trigger the actuators on Arduino. Typically, aPython program can be run on a laptop to process computer vision programs such as hand tracking using Google Mediapipe. With the integration with Arduino, you can then do the hand tracking to control remote robotics arms for instance. Isn't that this cool?
You will be writing a Python code on your laptop as the main processing platform and sending the command via UART USB serial communication to trigger the microcontroller which is connected to the laptop.
Note: The Python code can be running on single-board computers (SBCs) such as Raspberry Pi or NVIDIA Jetson Nano and trigger the actuators or read the sensors connected with microcontrollers.
What's Teachable Machine?Teachable Machine is a web-based tool that makes creating machine learning models fast, easy, and accessible to everyone - no expertise or coding required.
With Teachable Machine, users can train a computer to recognize images, sounds, & poses in only 3 steps as shown! That's very easy, isn't that?
If you are interested to understand in details regarding the 3 steps, you can view the videos below:
1. Teachable Machine Tutorial 1: Gather
2. Teachable Machine Tutorial 2: Train
3. Teachable Machine Tutorial 3: Export
Teachable Machine uses TensorFlow.js, a library for machine learning in Javascript, to train and run the models you make in your web browser.
Look at the Teachable Machine library built on top of TensorFlow.js on GitHub. Under the hood, these models use a technique called transfer learning. There’s a pretrained neural network, and when you create your own classes, you can sort of picture that your classes are becoming the last layer or step of the neural net. Specifically, both the image and pose models are learning off of pretrained mobilenet models
Watch the video introducing Teachable Machine!
Pyserial Python Module and Arduino CodeWhile we can use Python to control external hardware, the popular Python module that you can use is Pyserial. You can install it by the command
pip install pyserial
Note: While we install the Pyserial module using pip install pyserial, we will import the Pyserial module using:
import serial
Python Code on Laptop
Let's begin with a simple Python script that turn the LED on and off.
# arduino_blink.py
import serial
import time
for i in range(10):
with serial.Serial('COM4', 9800, timeout=1) as ser:
time.sleep(0.5)
ser.write(b'H') # send the pyte string 'H'
time.sleep(0.5) # wait 0.5 seconds
ser.write(b'L') # send the byte string 'L'
Few things to note:
- byte string
b'H'
is sent to the Arduino, not the unicode string 'H'. The unicode string 'H' is pre-pended with the letter b in the lineser.write(b'H')
. - This
'COM#'
is the COM port we found under the Windows Device Manager. It may be different number depending on your own setup.
<put a photo for COM port>
For Python code, the main thing that it does is by sending the byte string b'H'
and b'N'
to the Arduino via the COM port attached to the laptop.
Here are some additional reference materials to enhance your understanding of Pyserial module usage.
2. Python and External Hardware
h. Using Python to Control Arudino
Arduino Code
While for Arduino side, open the Arduino sketch PhysicalPixel.ino
by going to File → Examples → 04.Communication → PhysicalPixel
/*
Physical Pixel
An example of using the Arduino board to receive data from the computer. In
this case, the Arduino boards turns on an LED when it receives the character
'H', and turns off the LED when it receives the character 'L'.
The data can be sent from the Arduino Serial Monitor, or another program like
Processing (see code below), Flash (via a serial-net proxy), PD, or Max/MSP.
The circuit:
- LED connected from digital pin 13 to ground
created 2006
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe and Scott Fitzgerald
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/PhysicalPixel
*/
const int ledPin = 13; // the pin that the LED is attached to
int incomingByte; // a variable to read incoming serial data into
void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
// see if there's incoming serial data:
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
// if it's a capital H (ASCII 72), turn on the LED:
if (incomingByte == 'H') {
digitalWrite(ledPin, HIGH);
}
// if it's an L (ASCII 76) turn off the LED:
if (incomingByte == 'L') {
digitalWrite(ledPin, LOW);
}
}
}
For the Arduino side, Arduino boards turn on an LED when it receives the character 'H', and turn off the LED when it receives the character 'L' from the laptop via UART serial communication.
ConclusionWith the combination of Python and Teachable Machine, you could make use of your laptop to run the ML algorithm with Python and then trigger the actuators connected to the laptop via USB cable or Bluetooth/WiFi connection.
Here are some of the interesting projects you can achieve by Murtaza's Workshop - Robotics and AI, and Rizky Dermawan and myself.
- [Demo] PWM Control using Hand Gesture | Mediapipe | OpenCV | Arduino
- AI ROBOT ARM using Python Arduino OpenCV CVZone | Computer Vision
- Voice Activated Robo Car on Microcontroller with TinyML
Feel free to leave a comment if you find some interesting use cases around Python + Arduino.
CreditThis post is done in collaboration with PCBWay. PCBWay is providing services for one-stop PCB, and PCBA services. Furthermore, if you have some good ideas for the hardware, they can help to manufacture and sell them within their PCBWay shop! Read more from this link. PCBWay also provide the platform for makers to share their open-sourced projects as well. Check out the different projects on their "Share & Discover" website.
Thank you.
Comments
Please log in or sign up to comment.