Wed. Aug 12th, 2020

How To Use Collaboratively Arduino With Raspberry-Pi

6 min read

Attach an Arduino board to your Raspberry Pi and use unique features such as an integrated ADC slot.

Two separate devices are Arduinos and Raspberry Pi. All are designed for passionate hackers and builders. What about connecting an Arduino to a Raspberry Pi and using it as a slave device: one that responds to the input and sends the output via Python to our Raspberry Pi?

After all, while Arduino is not a full-fledged device, it has some good stuff, such as analog-to-digital conversion with its integrated ADC chip. We will need some special software to link the Arduino to the Raspberry Pi, and this is why this tutorial begins.

Setup of Applications To Use Arduino and Raspberry:

We have to download Arduino IDE for 32-bit Linux ARM before we can write any Python code. Once it is enabled, the user “pi” must be added to the right category to submit Arduino data. Open a program and type the following to add pi to the group:

                                     $ sudo usermod -a -G dialout pi

Then reboot your Raspberry Pi before continuing. With the Pi rebooted, open Arduino IDE, and select File > Examples > Basic > Blink and then go to Tool > Board and select your board. For our tests, we used an Arduino Uno.

Then go to Tools > Port and ensure that the port is selected on your screen. Tap Sketch > Upload to import the code into the Arduino (or tap on the arrow in the menu). The integrated Arduino LED will flash on / off slowly after a few seconds. This shows that we have a job team.

Now we can flash a special draft that will allow us to talk with Python to our Arduino. Go to File > Examples > Firmata > StandardFirmatas and show your Arduino with this sketch. If flashed, the Arduino IDE can also be locked. Open the new terminal and type the following to install the pyFirmata library:

                                 $ sudo pip3 install pyfirmata

Hello World Project on Arduino & Raspberry Pi

We will write a short script to trigger an LED connected with the pin 12 of the Arduino in order to check whether our Arduino will work with Python. For the links please see the diagram.

Plug the following code into the Arduino and the terminal form. Find USB apps like ttyUSB0 and ttyACM0. Please write a note before continuing.

                               $ dmesg

Build a new file and call it LED test.py with your favorite Python 3 editor (IDLE, Thonny, Nano, Vim). We’re now going to write in this file a Python version. Begin by importing two classes from the pyFirmata library that can link to the Arduino with our code. Then

                              from pyfirmata import Arduino, util
from time import sleep

the sleep function can be imported by type from the time library: the next step is to create an object called a board that will connect our Pi to the Arduino. We will use USB device data from dmesg for this reason. Our Arduino was at ttyUSB0, in our situation.

                             board = Arduino(‘/dev/ttyUSB0’)

A variable called led is used to store the Arduino pin number. You create it by adding the line:

                            led = 12\

We can write a program within some true loop, and every 0.2 seconds, the LED will turn on and off. We call the object box and then write 1 to the pin to allow it with a class to digitally monitor the pin (0,1). Mind, we are using the pin identifier variable. Then we sleep 0.2 seconds before shutting off the pin and again sleeping.

                        while True:
board.digital[led].write(1)
sleep(0.2)
board.digital[led].write(0)
sleep(0.2)

You can then save the file, run it from the editor and then the LED connecting to Arduino will flash, proving we have a working connection within a matter of second (IDLE Run > Run Module / Thonny Run > Run Curry Script).

This will be the ultimate python script:

                  from pyfirmata import Arduino, util
from time import sleep
board = Arduino(‘/dev/ttyUSB0’)
led = 12
while True:
board.digital[led].write(1)
sleep(0.2)
board.digital[led].write(0)
sleep(0.2)

Let’s create a new project now. The interval would be an analog electronic component, which the Raspberry Pi would usually not use without the introduction of analog to digital conversion boards, but the interval between each flash is regulated by an analog electronic potentiometer.

We can control the speed at which the LED blows by using the Arduino returned value. The potentiometer we have just designed and tested will be connected to the existing LED test circuit. For more detail on this, please see the diagram below.

In a new blank tab, we will begin importing and setting up the pin used on Arduino with the same row.

            from pyfirmata import Arduino, util
from time import sleep
board = Arduino(‘/dev/ttyUSB0’)
led = 12

We need to build a thread that runs and not interrupts the key code for reading the analog values from the Arduino. We must build an object named it, then connect it to the Arduino and start the thread.

          it = util.Iterator(board)
it.start()
board.analog[0].enable_reporting()

The core body is a True Loop, which reads and stores information in a variable known as the information of the present analog pin 0 connected to the potentiometer. It is then written to the shell of Python.

            while True:
value = board.analog[0].read()
print(value)

The meaning variable is now subjected to a conditional check. If the value has no data, none is returned and the code crashes. Therefore if the condition tests the value, it sets it to 0 if it is zero.

           if value == None:
value = 0

The analog values returned are between 0.0 and 1,0 if the value is greater than 0.05, this must also be checked. The LED is allowed if the value reaches 0.05 and the sleeping interval is managed by the value, which is used to hold the LED on and on when the code is pause.

          elif value > 0.05:
board.digital[led].write(1)
sleep(value)
board.digital[led].write(0)
sleep(value)

Flashing LED Lights with Raspberry Pi and Arduino

Another feature is the final code lines that turns the LED off when the value is lower than 0.05. Save and execute the file. Turn the potentiometer and see the LED come alive. This is how the last code of your python will look:

            from pyfirmata import Arduino, util
from time import sleep
board = Arduino(‘/dev/ttyUSB0’)
led = 12
it = util.Iterator(board)
it.start()
board.analog[0].enable_reporting()
while True:
value = board.analog[0].read()
print(value)
if value == None:
value = 0
elif value > 0.05:
board.digital[led].write(1)
sleep(value)
board.digital[led].write(0)
sleep(value)

 

Written by: Nimra Siddiqui

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © All rights reserved.