How to Use Resistive Sensors in Raspberry pi

You need to connect a variable resistor to a Raspberry Pi and measure its resistance so that you simply can utilize the position of the variable resistor’s knob in your Python program. Today I show you How to How to Use Resistive Sensors in Raspberry pi 

Components Required :

1.Raspberry pi

2.Breadboard

3. Connecting wire

6. Resistors

7. 5K ohm Trim Pot

This book will help you to gain more knowledge of Raspberry pi  Software and Hardware Problems and Solutions

Raspberry Pi Cookbook

Circuit diagram:

shows the arrangement of components on the breadboard

Circuit diagram Circuit diagram

You'll be able to measure the resistance on a Raspberry Pi using nothing more than a capacitor, a few resistors, and two GPIO pins. In this case, you'll be able to gauge the position of the handle on a small variable resistor by measuring its resistance from its slider contact to one end of the pot.

Code Resistive Sensors Raspberry pi:

import RPi.GPIO as GPIO
import time, math

C = 0.36 # uF
R1 = 1000 # Ohms

GPIO.setmode(GPIO.BCM)

# Pin a charges the capacitor through a fixed 1k resistor 
# and the pot in series
# Pin b discharges the capacitor through a fixed 1k resistor 
a_pin = 18
b_pin = 23

# Discharge the capacitor, leaving it ready to start filling up
def discharge():
    GPIO.setup(a_pin, GPIO.IN)
    GPIO.setup(b_pin, GPIO.OUT)
    GPIO.output(b_pin, False)
    time.sleep(0.1)

# Return the time taken (uS) for the voltage on the capacitor 
# to count as a digital input HIGH
# which is 1.65V or higher
def charge_time():
    GPIO.setup(b_pin, GPIO.IN)
    GPIO.setup(a_pin, GPIO.OUT)
    GPIO.output(a_pin, True)
    t1 = time.time()
    while not GPIO.input(b_pin):
        pass
    t2 = time.time()
    return (t2 - t1) * 1000000

# Take an analog reading as the time taken to charge after 
# first discharging the capacitor
def analog_read():
    discharge()
    t = charge_time()
    discharge()
    return t

# Convert the time taken to charge the cpacitor into a value of resistance
# To reduce errors, do it 100 times and take the average
def read_resistance():
    n = 10
    total = 0;
    for i in range(1, n):
        total = total + analog_read()
    t = total / float(n)
    T = t * 0.632 * 3.3
    r = (T / C) - R1
    return r
try:
    while True:
        print(read_resistance())
        time.sleep(0.5)
finally:
    GPIO.cleanup()

When you run the program, you should see some output like this:

10232.2157936
10155.1559448

The reading will shift as you turn the knob of the trimpot. the reading of resistance would change between and 10,000Ω but in practice, there will be a few mistakes. You might like to change the value of the constant C at the top of the program in the event that you need more exact readings, but remember the value of C will probably have to be changed in the event that you put another capacitor in, indeed in case the capacitor was nominal of the same value

If you want to know more about raspberry pi then click on the link below

RASPBERRY PI TUTORIALS FOR BEGINNERS

No comments

Theme images by Dizzo. Powered by Blogger.