Arduino Raspberry Pi achieve control by pyFirmata

Author: Rishabh Jain
compilation: Tony

Raspberry Pi control Arduino

Although the terms of its application and structure, Raspberry Pi and Arduino are two different hardware, but they are considered to be two competing open-source hardware platform. They both have a very strong community and support. Today we will make some changes, and show you how to use them. If you have both Arduino and Raspberry pi board, then this article will show you how to use the Raspberry pi and Python control Arduino .

We will use PyFirmata firmware sends commands to Arduino by Raspberry Pi python script. PyFirmata essentially, the program constructs python library package, which can be mounted on Arduino to allow serial communication between the python and the Arduino script on any computer. The python package can access any of the pins on the Arduino. So, here we will use the Raspberry pi python program running on the Arduino.

Thus, in this tutorial, we will use the library, and use it to control the development board Arduino Arduino Pi by the Raspberry .

Claim

  • Raspbian operating system installed Raspberry Pi
  • Arduino Uno or any other Arduino development board
  • Arduino USB Line
  • led

In this tutorial, I will connect with Raspberry Pi using an external monitor via the HDMI cable. If no monitor, you can use the SSH client (as Putty) or VNC server is connected to Raspberry pi or laptop computer.

Raspberry Pi PyFirmata mounted in use in Arduino

To upload firmware PyFirmata Arduino, we must install Arduino IDE in the Raspberry Pi. Install in accordance with the following steps:

  • * Step 1: Connect to the Internet ** Raspberry Pi. Open Terminal and type the following command, and then press Enter
sudo apt-get -y install arduino python-serial mercurial

Wait a few minutes, it will take some time. This command will install Arduino IDE in your Raspberry Pi.

  • ** Step 2: ** Now, we will use the specified github pyFirmata installation file:
git clone  https://github.com/tino/pyFirmata

Then run the following command:

cd pyFirmata 
sudo python setup.py install
  • Step 3: We have all the necessary files and settings installed.

Now, using the USB cable Arduino Raspberry Pi Boards and connected, and then type in the IDE Arduino arduino start terminal window.

Start arduino

  • ** Step 4: - ** and type lsusb command to check if the connection with Arduino Raspberry Pi.

In the Arduino IDE, go to Tools, then select the board and serial port.

  • Step 5 **: ** by clicking File -> Example -> Firmware -> standard firmware, and then click the upload button to upload the firmware on the Arduino PyFirmata, as shown below.

Upload Firmware

We have pyFirmata Arduino board firmware successfully installed. Now, we can use the Raspberry Pi control Arduino.

To demonstrate, we will make flashing LED on the Arduino and graded by writing python code in Raspberry Pi.

Code Description

For coding section, you should read the pyFirmata documentation to better understand, we will use pyFirmata function code.

Open your favorite text editor on your Raspberry Pi, and then import pyFirmata library.

import pyfirmata

Arduino defined in the pin to connect the LED

led_pin = 9

Now, we have to use pyfirmata.Arduino () function to write connection Arduino board's serial port name , then create an instance of the port assignment by the board variable.

board = pyfirmata.Arduino("/dev/ttyACM0")
print "Code is running”

In the while loop, using board.digital []. Write () function led pin is set high and low, using board.pass_time function to provide delay.

while True:
    board.digital[led_pin].write(0)
    board.pass_time(1)        
    board.digital[led_pin].write(1)
    board.pass_time(1)

Our code is ready to save the file to the code by the .py extension.

Open a command terminal, and type python blink.py to run the code Arduino board. Be sure to use a USB cable is connected to the development board Arduino Raspberry Pi boards.

Now you can see the LED flashes Arduino board.

PyFirmata graded using the LED on the Arduino

Now, we'll write the code for the LED gradient to make you more familiar with pyFirmata function. This code is the same as with a simple code. You must use two for loops, one for increasing the brightness and one for the lower brightness.

In this code, we define the pins in different ways, such as LED = board.get_pin ( 'd:. 9: P') , wherein d represents the digital pins. This is a functional pyFirmata library, part of the code as follows:

led = board.get_pin('d:9:p')            
while True:
# increase
    for i in range(0, 10):
        brightness = brightness + 0.1
        print "Setting brightness to %s" % brightness
        led.write(brightness)
        board.pass_time(delay)

Now you can add more sensors in the system, try to use the Raspberry pi and python scripts to build them.

phenomenon

Attach the complete code:

#Python code for LED blink:
import pyfirmata
led_pin = 9
board = pyfirmata.Arduino("/dev/ttyACM0")

while True:
    board.digital[led_pin].write(0)
    board.pass_time(1)
    board.digital[led_pin].write(1)
    board.pass_time(1)

#Python code for Fading LED:
import time
import pyfirmata

delay = 0.3
brightness = 0
board = pyfirmata.Arduino("/dev/ttyACM0")
led = board.get_pin('d:9:p')    
        
while True:
# increase
    for i in range(0, 10):
        brightness = brightness + 0.1
        print "Setting brightness to %s" % brightness
        led.write(brightness)
        board.pass_time(delay)

# decrease
    for i in range(0, 10):
        print "Setting brightness to %s" % brightness
        led.write(brightness) 
        brightness = brightness - 0.1     
        board.pass_time(delay)

Focus on micro-channel public number: TonyCode
the Arduino learning exchange group: 868 283 450

More, I welcome the attention of the public number. Sweep the micro-channel to follow the Fanger Wei code:
Micro channel scan code added public number: TonyCode

Published 63 original articles · won praise 250 · Views 230,000 +

Guess you like

Origin blog.csdn.net/TonyIOT/article/details/103108413