EXPERIMENT:
Interface motor using relay with Raspberry Pi, write a program to turn ON motor when
push button is pressed.
Servo Motor:
Program:
import RPi.GPIO as IO # calling for header file for GPIO’s of PI
import time # calling for time to provide delays in program
IO.setwarnings(False) # do not show any warnings
IO.setmode (IO.BCM) # programming the GPIO by BCM pin numbers.
IO.setup(19,IO.OUT) # initialize GPIO19 as an output
p = IO.PWM(19,50) # GPIO19 as PWM output, with 50Hz frequency
p.start(7.5) # generate PWM signal with 7.5% duty cycle
while 1: # execute loop forever
p.ChangeDutyCycle(7.5) # change duty cycle for getting the servo position to 90º
time.sleep(1) # sleep for 1 second
p.ChangeDutyCycle(12.5) # change duty cycle for getting the servo position to 180º
time.sleep(1) # sleep for 1 second
p.ChangeDutyCycle(2.5) # change duty cycle for getting the servo position to 0º
time.sleep(1) # sleep for 1 second
RESULT:
The motor will turn on when the push button is pressed..
EXPERIMENT:
Interface soil moisturizer sensor using relay with Raspberry Pi, write a program to find the
amount of moist in soil.
Soil Moisturizer Sensor
➢ A soil moisture sensor is a device used to measure the moisture in the soil. The sensor works
by sending out an electromagnetic signal that is transmitted through the soil. The signal is then
received by the sensor and the moisture content is measured.
➢ The sensor can be used to monitor the moisture content in the soil and then the irrigation system
can be adjusted accordingly.
➢ This can help to reduce the amount of water that is used and can also help to reduce the amount
of water that is wasted.
The Probe
➢ Two exposed conductors on a fork-shaped probe are put wherever moisture levels need to be
determined.
➢ Its function is that of a variable resistor whose resistance changes as a function of soil
moisture.
The Module
➢ The sensor also has an electronic module that interfaces the probe.
➢ The module produces a voltage proportional to the probe's resistance and makes it available
through an Analog Output pin.
➢ The same signal is then sent to a Digital Output pin on an LM393 High Accuracy
Comparator, which is digitized.
➢ The module features a potentiometer (DO) for fine-tuning the digital output sensitivity.
➢ It can be used to establish a threshold, at which point the module will output LOW if the
moisture level exceeds the threshold and HIGH otherwise.
➢ This setup works great for initiating an event when a given value is reached. For instance,
a relay might be set off to begin watering the plant if the soil moisture level rises above a
given threshold.
➢ In addition to the IC, the module has two LEDs. When the component is activated, the
Power LED will light up, and the Condition LED will light up if the moisture level is above
the setpoint.
Pinout of A Soil Moisture Sensor
Four pins are included in the soil moisture sensor.
Circuit Diagram
➢ VCC -> 5V
➢ GND -> GND
➢ DATA-> GPIO 4
Python Code:
import RPi.GPIO as GPIO
import time
#GPIO SETUP
channel = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.IN)
def callback(channel):
if GPIO.input(channel):
print ("Water Detected!")
else:
print ("No Water Detected!")
GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300)
GPIO.add_event_callback(channel, callback)
while True:
time.sleep(0)
OUTPUT:
No Water Detected!
Water Detected!
No Water Detected!
Water Detected!
Water Detected!
No Water Detected!
RESULT:
Interfacing with soil moisturizer sensor using a relay with Raspberry Pi to find the amount
of moisture in the soil.
EXPERIMENT:
Interface LCD with Raspberry Pi, write a program to print temperature and humidity readings
on it.
Circuit Diagram
Installing the Adafruit LCD library on Raspberry Pi:
Step 1: Install git on your Raspberry Pi by using the below line.
apt-get install git
Step 2: Execute the line to clone the project file on Pi home directory
git clone git://github.com/adafruit/Adafruit_Python_CharLCD
Step 3: Use the below command to change directory line
cd Adafruit_Python_CharLCD
Step 4: Inside the directory there will be a file called setup.py. Use the following code to install the
library
sudo python setup.py install
Installing the Adafruit DHT11 library on Raspberry Pi:
Enter the four command lines one by one on the terminal to install the DHT library
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
cd Adafruit_Python_DHT
sudo apt-get install build-essential python-dev
sudo python setup.py install
Program:
import Adafruit_CharLCD as LCD #Import LCD library
import Adafruit_DHT #Import DHT Library for sensor
sensor_name = Adafruit_DHT.DHT11 #we are using the DHT11 sensor
sensor_pin = 17 #The sensor is connected to GPIO17 on Pi
lcd_rs = 7 #RS of LCD is connected to GPIO 7 on PI
lcd_en = 8 #EN of LCD is connected to GPIO 8 on PI
lcd_d4 = 25 #D4 of LCD is connected to GPIO 25 on PI
lcd_d5 = 24 #D5 of LCD is connected to GPIO 24 on PI
lcd_d6 = 23 #D6 of LCD is connected to GPIO 23 on PI
lcd_d7 = 18 #D7 of LCD is connected to GPIO 18 on PI
lcd_backlight = 0 #LED is not connected so we assign to 0
lcd_columns = 16 #for 16*2 LCD
lcd_rows = 2 #for 16*2 LCD
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7,
lcd_columns, lcd_rows, lcd_backlight) #Send all the pin details to library
lcd.message('DHT11 with Pi \n -CircuitDigest') #Give a intro message
time.sleep(2) #wait for 2 secs
while 1: #Infinite Loop
humidity, temperature = Adafruit_DHT.read_retry(sensor_name, sensor_pin)
#read from sensor and save respective values in temperature and humidity varibale
lcd.clear() #Clear the LCD screen
lcd.message ('Temp = %.1f C' % temperature) # Display the value of temperature
lcd.message ('\nHum = %.1f %%' % humidity) #Display the value of Humidity
time.sleep(2) #Wait for 2 sec then update the values
OUTPUT:
(Reading temperature and humidity...)
Temperature: 25.6°C
Humidity: 60.2%
Temperature: 25.7°C
Humidity: 60.4%
RESULT:
To display temperature and humidity readings, use an LCD interface with a Raspberry Pi.
EXPERIMENT:
Write a program on Raspberry Pi to upload temperature and humidity data to the cloud.
Program:
from firebase import firebase
import RPi.GPIO as GPIO
import dht11
import time
url="https://temp-7bca8-default-rtdb.firebaseio.com/Weather"
firebase = firebase.FirebaseApplication(url)
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
instance = dht11.DHT11(pin=2)
while True:
result = instance.read()
if result.is_valid():
print(“Temp: %d ” % result.temperature + ‘ ‘ +”Humid:%d %%”% result.humidity)
temp = firebase.put(“Weather”,”Temp”, result.temperature)
humid = firebase.put(“Weather”, “Humid”, result.humidity)
time.sleep(1)
Sample Output:
>>> %Run temp.py
Temp: 35 Humid:48%
Temp: 35 Humid:48%
Temp: 35 Humid:48%
Temp: 35 Humid:48%
Temp: 35 Humid:48%
Upload temperature and humidity data to Firebase Cloud.
RESULT:
Raspberry Pi to upload temperature and humidity data to the cloud.
EXPERIMENT:
Write a program on Raspberry Pi to retrieve temperature and humidity data from cloud.
RESULT:
Raspberry Pi retrieve temperature and humidity data from cloud.