How to interface Arduino with RaspberryPi

How to interface Arduino with RaspberryPi © GPL3+

In this I have shown how to send data from temperature sensor which is connected to Arduino and send data to Rpi database.

  • 10,551 views
  • 1 comment
  • 14 respects

Components and supplies

Apps and online services

About this project

How to interface Arduino with RaspberryPi

1. Download and install Arduino in Rpi

Open browser in raspberry pi and open the link below.

https://www.arduino.cc/en/Main/OldSoftwareReleases#previous

Then, download Linux ARM and extract it by command.

tar  -xvzf file name  

After extracting you will see new directory. Here I am using arduino-1.8.1 IDE. Then go to directory by using command.

cd arduino-1.8.1 

To run Arduino, use command in arduino-1.8.1 directory.

 ./arduino 

2. How to use libraries

To install any libraries in Arduino , simply download the library and paste in arduino--->libraries folder .

NOTE:- Make sure there is no ( - ) in library folder for ex (DHT-sensor). If there is any (-),rename it .

Now let's do one example of temperature sensor. To check whether its working or not.

Here, I am interfacing DHT11 temperature sensor with Arduino uno.

===================================================================

**************************************************************************************************

Arduino Program

===================================================================

**************************************************************************************************

#include "DHT.h"

#define DHTPIN 7 // what digital pin we're connected to

// Uncomment whatever type you're using!

#define DHTTYPE DHT11 // DHT 11

//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321

//#define DHTTYPE DHT21 // DHT 21 (AM2301)

// Initialize DHT sensor.

DHT dht(DHTPIN, DHTTYPE);

void setup() {

Serial.begin(9600);

dht.begin();

}

void loop() {

// Wait a few seconds between measurements.

delay(2000);

// Reading temperature or humidity takes about 250 milliseconds!

int h = dht.readHumidity();

// Read temperature as Celsius (the default)

int t = dht.readTemperature();

Serial.print("Humidity: ");

Serial.print(h);

Serial.print("\t"); // for splitting

Serial.print("Temperature: ");

Serial.print(t);

Serial.print("\n"); // for new line

}

======================================================================

*******************************************************************************************************

Verify it and upload it to Arduino uno via Raspberry pi.Then open serial monitor .

We have verified its working ,now proceed to next step.

3. Store temperature data to database of raspberry pi .

Here I am assuming you already have created database in raspberry pi. If you dont know how to create database,check link below.

http://www.raspberry-projects.com/pi/software_utilities/web-servers/mysql

After that install the required libraraies.

Sudo apt-get install python-serial 

Sudo apt-get install python-mysqldb 

After installing libraries. Create a python script to store data from Arduino to Raspberry pi database.

Sudo nano name.py 

Program to be write in script

=====================================================================

*****************************************************************************************************

import serial

import time

import MySQLdb as mdb

arduino = serial.Serial("/dev/ttyACM0") // make sure you write correct serial

arduino.baudrate=9600

data = arduino.readline()

time.sleep(2)

data = arduino.readline()

pieces =data.split("\t")

temperature = pieces[0]

humidity = pieces[1]

con = mdb.connect('localhost','root','password','database_name');

with con:

cursor =con.cursor()

cursor.execute("INSERT INTO table_name VALUES('',%s,%s

(temperature,humidity))

con.commit()

cursor.close()

======================================================================

*******************************************************************************************************

Run python script by command

python ./scriptname.py 

To run python script automatically after few seconds to update database ,we have to change crontab and save it . To open crontab ,write command.

Crontab –e 

After opening it ,write */1 * * * * python /home/pi/scriptname.py at the end .

After edit and save crontab. Reboot raspberry pi.

======================================================================

Result

======================================================================

NOTE:- ALL THE THINGS WORK ONLY WHEN TEMPERATURE SENSOR IS CONNECTED TO ARDUINO AND ARDUINO IS CONNECTED TO RASPBERRY PI SERIALLY.

Code

Arduino codeArduino
Use it in Arduino IDE of RPi
#include "DHT.h"
#define DHTPIN 7 // what digital pin we're connected to
// Uncomment whatever type you're using!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Initialize DHT sensor.
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!

int h = dht.readHumidity();
// Read temperature as Celsius (the default)
int t = dht.readTemperature();
Serial.print("Humidity: ");
Serial.print(h);
Serial.print("\t"); // for splitting
Serial.print("Temperature: ");
Serial.print(t);
Serial.print("\n"); // for new line
}
Python ScriptPython
import serial
import time
import MySQLdb as mdb

arduino = serial.Serial("/dev/ttyACM0") // make sure you write correct serial
arduino.baudrate=9600
data = arduino.readline()
time.sleep(2)
data = arduino.readline()
pieces =data.split("\t")
temperature = pieces[0]
humidity = pieces[1]
con = mdb.connect('localhost','root','password','database_name');
with con:
  cursor =con.cursor()
  cursor.execute("INSERT INTO table_name VALUES('',%s,%s)",(temperature    ,humidity))
  con.commit()
  cursor.close()

Schematics

rpi-arduino_H0rZGlwtkh.png
Rpi arduino h0rzglwtkh
arduino-dht113pindht11wiringdiagram_ytOJpEGUwv.png
Arduino dht113pindht11wiringdiagram ytojpeguwv
arduinodht114pindht11wiringdiagram_xaJhriIM4q.png
Arduinodht114pindht11wiringdiagram xajhriim4q
dht11-pinout_ISuDqk7EIn.jpg
Dht11 pinout isudqk7ein

Comments

Similar projects you might like

How to Interface GPS Module (NEO-6m) with Arduino

Project tutorial by Ruchir Sharma

  • 120,713 views
  • 104 comments
  • 54 respects

Arduino + Raspberry Pi + Ubidots Made Easy

Project tutorial by David Escobar

  • 13,489 views
  • 4 comments
  • 40 respects

Light Control Using Arduino and Amazon Alexa

Project tutorial by Rajesh

  • 14,216 views
  • 1 comment
  • 16 respects

Interface RFID Shield With Arduino

by bhaskar

  • 3,097 views
  • 0 comments
  • 13 respects
Add projectSign up / Login