Arduino and Python
In this introduction, I have worked with an older version of Arduino Nano without having any other hardware parts or LEDs to plugged in. The idea behind this step-by-step explanation was to make some exercise and to find the way how to control Arduino not directly with Arduino IDE software and it's a subset of the
C/C++ standard library language, but control it with
Python script. To make it real we will need these mentioned above anyway, but just partially.
Simply said we’re going to send a very simple signal with a
Python script to the Arduino LED - just a signal to turn an LED on or off like this.
Be noted this tutorial can be applied on any other Linux based system or Windows too.
Install Official Arduino IDE
Let's start simply downloading a ZIP file for Mac OS X from the official Arduino website. We gonna use it for uploading some sort of
C/C++ library into the device, to allow
Python to communicate with our Arduino Nano. Unzip file and drop,
Arduino.app into
Applications directory to keep it nice and organized. Start this app for the first time and you can close the first window with a short script example that will pops-up right after. Head into
File > Examples > Firmata > StandardFirmata as you see on screenshot below.
This script will show up in the next window.
Plug in Arduino and Upload The Script
Before this step, you definitely need to plug in your Arduino to a computer or laptop. You'll need Mini USB known as Mini B 5 pin on one end and the other end with USB 2 A cable. You can look for some reduction to this end-point, based on the model of the MacBook you are using to test.
Now the only thing you need to do is press the upload button (arrow pointing to the right) on the very top part of the window.
You'll see in the bottom part of the window information "Done uploading." and in the bottom console output like this.
Firmata script has been uploaded into Arduino Nano and now you can close the whole
Arduino.app. No need to work with it at all.
Python Library For Communication With Arduino
I have been trying to run this whole thing with pyFirmata, but I have been facing issues with my
Python script to detect the Arduino model, so I have a follow-up with pyFirmata2 and it worked well. To install it quickly and smoothly just enter this code into the console.
pip3 install pyfirmata2
Notes Below Regards To Pip3 Installation
In case the
Pip3 installation above doesn't work for you properly, the main reason should be you don't have installed
Pip3 and
Python at all. Visit brew.sh and copy-paste the code listed on the website.
/bin/bash -c "$(curl -fsSL)"
Then after install python3 with
brew install python3.
brew install python3
And for now try
pip3 install pyfirmata2 to install
pyfirmata2.
Python Script To Blink Arduino
To start playin' with Arduino and see that blinking magic visit link to the script below and press
CTRL + S to save it on Desktop for example. This script will light up LED labeled on board as L (in script we call this LED with
board.digital[13] code.
👉 Download Python Blink Script
Or the whole script you can find here too.
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
To run this script just head to Desktop and run it from the terminal with these commands hereunder.
cd Desktop python3 blink-arduino.py
You'll see status info in a terminal like this one.
...Arduino detected ...Blink test started
Now LED should light up 4 times for 2-seconds with a 1-second pause when it'll light off and in the terminal, you should be awarded by status info like this every second as it light off.
...Blink LED 1st time ...Blink LED 2nd time ...Blink LED 3rd time ...Blink LED 4th time
After this whole little blink ceremony, you'll see this info.
...Blink test successfull!
Final Words
Feel free to play around with the script to change the frequency and length of LED lighting and that's pretty everything done with this small script exercise I play around with.
Thanks to Mathew Schwartz for the cover image from Unsplash.
Discussion (0)