group-icon
Arduino + Raspberry Pi Lovers
Discussion and Q&A about Arduino and Raspberry Pi projects.
177 Members
Join this group to post and comment.
Ashwani Kumar
Ashwani Kumar
Electronics and Communication
12 Feb 2019

How to interface Arduino UNO with Raspberry Pi ?

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

Ashwani Kumar
Ashwani Kumar
Electronics and Communication
1mo ago

Scroll below


How to interface Arduino with RaspberryPi

How to interface Arduino with RaspberryPi 

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

Requirements:-

Arduino, Raspberry Pi, dht11,arduino ide.

first download and install Arduino ide.


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.