Skip to main content

Arduino UNO and Python

When we talk about microcontrollers, like Arduino, the first thing that comes to mind is "C/C++".

To write something complex, one needs to know C/C++. But is that really the case?

I just want to share how I do everything I want with Arduino using Python.

Why Python? 

Why choose Python, you might ask? One of the reasons is the availability of numerous libraries that are already developed, making integration with controllers super fast.

For instance, I've integrated MediaPipe with my Arduino. The result? The LCD displays hand positions, and using 5 LEDs, I can indicate the number of fingers I'm showing with my hand.

  • When I show 0 fingers, all lights are off. 
  • For 1 finger, 1 LED lights up. 
  • For 2 fingers, 2 LEDs light up. 
  • For 3 fingers, 3 LEDs light up. 
  • For 4 fingers, 4 LEDs light up. 
  • And for 5 fingers, all 5 LEDs light up.

So, there you have it: a simple demonstration of integrating MediaPipe with Arduino using just one script.

Could you achieve this as quickly with C++? In just a few lines of code? 👵


Arduino Implementation Details:

Managing an Arduino from Python generally involves using serial communication. The Arduino communicates over USB with a serial protocol, and Python can interact with this protocol using the pyserial library.

The Arduino code should contain commands to initiate serial communication, typically using the Serial.begin() function.
The Arduino code should be written to interpret commands received over the serial port and perform corresponding actions.

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
This line initializes an instance of the LiquidCrystal class, specifying which Arduino pins are connected to the LCD's RS, E, D4, D5, D6, and D7 pins, respectively.


Python Serial Setup:
Install the pyserial library using pip: 
pip install pyserial.

Establish a connection to the Arduino using the serial.Serial() function. 

You'll need to specify the appropriate port and the baud rate, which should match what you've set on the Arduino.
Once you've established a serial connection, you can send commands to the Arduino from Python using the write() method.

Please share your thoughts and feedback if this topic resonates with you.

Thank you!