Tuesday, April 16, 2024
HomePythonConnecting and Interfacing Raspberry Pi with Arduino

Connecting and Interfacing Raspberry Pi with Arduino


Hello pretty folks! 👋 I’m engaged on a greenhouse monitoring mission and I needed to join my Arduino to the web over WiFi. I wished to push some sensor knowledge to my server. I may have purchased a WiFi defend for the Arduino however why spend extra money when you have already got a Pi with WiFi card?

I ended up connecting Arduino with my Pi and pushing sensor knowledge to my server by the Pi. The method was tremendous easy.

Step 1: Begin serial communication on Arduino

In your Arduino code you must begin serial communication throughout setup and ship some knowledge to the Raspberry Pi. I’m utilizing the loop operate to ship the information each half-second:

void setup(){
    Serial.start(9600);
}

void loop(){
    Serial.println("Welcome to Arduino :)");
    delay(500);
}

Add another code you may want to your mission and add the code on Arduino.

Step 2: Connecting Arduino & Raspberry Pi

Join the Arduino utilizing the USB cable to the Raspberry Pi after the code has been uploaded to Arduino.

Step 3: Allow I2C on the Raspberry Pi

We have to allow I2C communication on the Raspberry Pi. I2C is a typical customary which permits one system to speak to a different. You possibly can allow this from the terminal by working:

$ sudo raspi-config

Now choose 5 Interfacing Choices from the record. Now choose I2C from the record and choose allow.

Enabling I2C

Step 4: Write code on Pi to learn incoming knowledge

Create a file code.py in your desktop on the Raspberry Pi. Add the next code within the file:

import serial
import RPi.GPIO as GPIO
import time

ser=serial.Serial("/dev/ttyACM0",9600)  

whereas True:
    read_ser=ser.readline()
    print(read_ser)

The code above goes to open up a serial connection between the Arduino and Raspberry Pi. It would run a steady loop the place it can learn incoming knowledge from the Arduino and can print it on the terminal.

You is perhaps questioning the place I received /dev/ttyACM0 from. That is an identifier for the USB interface. It is perhaps barely totally different for you. As a way to discover out what this identifier is for you kind this command within the terminal:

$ ls /dev/tty*

This may print a complete bunch of stuff on the display:

/dev/tty    /dev/tty23  /dev/tty39  /dev/tty54      
/dev/tty0   /dev/tty24  /dev/tty4   /dev/tty55      
/dev/tty1   /dev/tty25  /dev/tty40  /dev/tty56      
/dev/tty10  /dev/tty26  /dev/tty41  /dev/tty57      
/dev/tty11  /dev/tty27  /dev/tty42  /dev/tty58      
/dev/tty12  /dev/tty28  /dev/tty43  /dev/tty59      
/dev/tty13  /dev/tty29  /dev/tty44  /dev/tty6       
/dev/tty14  /dev/tty3   /dev/tty45  /dev/tty60      
/dev/tty15  /dev/tty30  /dev/tty46  /dev/tty61      
/dev/tty16  /dev/tty31  /dev/tty47  /dev/tty62      
/dev/tty17  /dev/tty32  /dev/tty48  /dev/tty63      
/dev/tty18  /dev/tty33  /dev/tty49  /dev/tty7       
/dev/tty19  /dev/tty34  /dev/tty5   /dev/tty8       
/dev/tty2   /dev/tty35  /dev/tty50  /dev/tty9       
/dev/tty20  /dev/tty36  /dev/tty51  /dev/ttyACM0
/dev/tty21  /dev/tty37  /dev/tty52  
/dev/tty22  /dev/tty38  /dev/tty53  

Discover the string containing ACM and normally that’s going to be the interface for the Arduino.

Generally it can in all probability be ACM0 so that you don’t have to edit the code however whether it is totally different return and edit the code.py file.

Step 5: Run code.py

Good! We have now every thing we’d like. Its time to run code.py on the Raspberry Pi and knowledge ought to begin displaying up on the display.

Troubleshooting:

In case issues nothing exhibits up on the display reconnect the Arduino to your laptop computer, open up the serial monitor and make it possible for knowledge present up there a minimum of. If it doesn’t, there’s something fallacious together with your Arduino code.

If knowledge exhibits up within the serial monitor then restart your Raspberry Pi. Usually the age-old resolution of turning it on and off once more works wonders.

If it nonetheless doesn’t work, search on Google and if even Google isn’t in a position to assist, ship me an electronic mail and I’ll strive my greatest that can assist you out.


RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments