Arduino with Python on Mac OSX 📝
Tags: python, challenge, programming, electronics
Published: 2020-12-03 20:49:01

##Arduino and Python
In this introduction, I have worked with an older version of [Arduino Nano](https://store.arduino.cc/arduino-nano-every) 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 <code>C/C++</code> standard library language, but control it with <code>Python</code> 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 <code>Python</code> script to the Arduino LED - just a signal to turn an LED on or off like this.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zxxkzov3sbk4azbtgdsh.gif)

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](https://www.arduino.cc/en/software). We gonna use it for uploading some sort of <code>C/C++</code> library into the device, to allow <code>Python</code> to communicate with our Arduino Nano. Unzip file and drop, <code>Arduino.app</code> into <code>Applications</code> 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 <code>File > Examples > Firmata > StandardFirmata</code> as you see on screenshot below.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/25vbx5vqciukjqes3zhu.png)

This script will show up in the next window.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/yihzu2y0whojquwhbyqh.png)

##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.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lx6x5jp30zl1dk17aimu.png)

You'll see in the bottom part of the window information "Done uploading." and in the bottom console output like this.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/nukzq9n4nrj1jlivnzt3.png)

Firmata script has been uploaded into Arduino Nano and now you can close the whole <code>Arduino.app</code>. 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](https://pypi.org/project/pyFirmata/), but I have been facing issues with my <code>Python</code> script to detect the Arduino model, so I have a follow-up with [pyFirmata2](https://pypi.org/project/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 <code>Pip3</code> installation above doesn't work for you properly, the main reason should be you don't have installed <code>Pip3</code> and <code>Python</code> at all. Visit [brew.sh](https://brew.sh/) and copy-paste the code listed on the website.

```
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
```
Then after install python3 with <code>brew install python3</code>.

```
brew install python3
```
And for now try <code>pip3 install pyfirmata2</code> to install <code>pyfirmata2</code>.

##Python Script To Blink Arduino
To start playin' with Arduino and see that blinking magic visit link to the script below and press <code>CTRL + S</code> 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 <code>board.digital[13]</code> code.

👉 [Download Python Blink Script] (https://raw.githubusercontent.com/stankukucka/blink-arduino-python/main/blink-arduino.py)

Or the whole script you can find here too.

```python
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](https://unsplash.com/@cadop?utm_source=unsplash&utm_medium=referral&utm_content=CreditCopyTextGratitude) for the cover image from Unsplash.