0% found this document useful (0 votes)
52 views3 pages

IoT Tutorial-11 Solutions

Uploaded by

b21it118
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views3 pages

IoT Tutorial-11 Solutions

Uploaded by

b21it118
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Tutorial Solutions: Sensors and Measurements Using Raspberry Pi

This document provides a comprehensive guide to various measurement techniques using

Raspberry Pi. It covers resistive sensors, light sensors, voltage measurement, CPU temperature

monitoring, and distance measurement. Each section includes theoretical insights, wiring

instructions, Python code, and troubleshooting tips for smooth implementation.

1. How to Use Resistive Sensors?


Resistive sensors change their resistance in response to environmental conditions such as

temperature and movement. Examples include thermistors, potentiometers, and strain gauges.

Below is an example of using a thermistor with a voltage divider circuit. The output voltage is

measured using an ADC connected to the Raspberry Pi.

import spidev
import time

spi = spidev.SpiDev()
spi.open(0, 0)

def read_adc(channel):
adc = spi.xfer2([1, (8 + channel) << 4, 0])
value = ((adc[1] & 3) << 8) + adc[2]
return value

try:
while True:
value = read_adc(0) # Thermistor on Channel 0
print(f"Thermistor Value: {value}")
time.sleep(1)
except KeyboardInterrupt:
spi.close()

2. How to Measure Light with an LDR?


LDRs (Light Dependent Resistors) reduce resistance when exposed to light. You can measure light

intensity by using an LDR with a voltage divider connected to an ADC.

The following code demonstrates light measurement using MCP3008.


import spidev
import time

spi = spidev.SpiDev()
spi.open(0, 0)

def read_light(channel):
adc = spi.xfer2([1, (8 + channel) << 4, 0])
value = ((adc[1] & 3) << 8) + adc[2]
return value

try:
while True:
light_value = read_light(0)
print(f"Light Intensity: {light_value}")
time.sleep(1)
except KeyboardInterrupt:
spi.close()

3. How to Measure Voltage Using an ADC?


Since the Raspberry Pi cannot read analog inputs directly, an ADC like MCP3008 is used. This

example demonstrates how to measure an input voltage through an ADC channel.

import spidev

spi = spidev.SpiDev()
spi.open(0, 0)

def read_voltage(channel):
adc = spi.xfer2([1, (8 + channel) << 4, 0])
value = ((adc[1] & 3) << 8) + adc[2]
voltage = (value * 3.3) / 1023 # Convert to voltage
return voltage

try:
while True:
voltage = read_voltage(1)
print(f"Measured Voltage: {voltage:.2f} V")
except KeyboardInterrupt:
spi.close()

4. How to Measure the Raspberry Pi CPU Temperature?


The CPU temperature of the Raspberry Pi can be monitored using the system file located at

`/sys/class/thermal/thermal_zone0/temp`. The following code reads and prints the temperature.

def get_cpu_temp():
with open("/sys/class/thermal/thermal_zone0/temp", "r") as f:
temp = int(f.read()) / 1000 # Convert millidegrees to Celsius
return temp

try:
while True:
temp = get_cpu_temp()
print(f"CPU Temperature: {temp:.2f} °C")
time.sleep(1)
except KeyboardInterrupt:
pass

5. How to Measure Distance Using an Ultrasonic Sensor?


The HC-SR04 ultrasonic sensor measures distance by sending a pulse and calculating the time it

takes to bounce back. The following code demonstrates how to measure the distance using GPIO

pins.

import RPi.GPIO as GPIO


import time

GPIO.setmode(GPIO.BCM)
TRIG = 23
ECHO = 24

GPIO.setup(TRIG, GPIO.OUT)
GPIO.setup(ECHO, GPIO.IN)

def measure_distance():
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)

while GPIO.input(ECHO) == 0:
start = time.time()

while GPIO.input(ECHO) == 1:
end = time.time()

duration = end - start


distance = (duration * 34300) / 2 # Speed of sound = 34300 cm/s
return distance

try:
while True:
dist = measure_distance()
print(f"Distance: {dist:.2f} cm")
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()

You might also like