Reading analog values with digital pins

Reading analog values with digital pins Titelbild

24 Analoge Werte Digital Messen (LDR)

So far when when reading an input we have mainly used the gpio.input(pinNumber) and resulting in a reading of 1 or 0. This is really good when we want to read a button because a button only has two states, pressed (1 or high) or not pressed (0 or low). These are digital values which we can measure quiet easily with the Raspberry Pi. We measure a 1, or high, if the measured voltage at the pin is around 2 volts. There are many situations in this world however where we are dealing with data consisting of more than just 1 or 0. In particular we are referring to situations where we need to measure analog values at the gpio pins. We can’t measure these analog values directly from the Raspberry Pi pins, but we can get use some workarounds to get the values we need such as using a capacitor.

Here are the following components we need:

Photoresistor show Capacirot show

Let’s first discuss the photoresistor. The main difference between the photoresistor and a normal resistor is that the resistance isn’t always the same! In a dark room the resistor has a resistance of a few mega-Ω (that is a lot).This resistance changes however as the brightness of the light in the room changes. Next we need the help of a capacitor. A capacitor can save electrical energy (when it is charging) but can also release electrical energy (when it is discharging). You can imagine it as a battery pack with really a small amount of energy that can get stored. You can measure the capacity of a capacitor in farads or for short: F.

enter image description here

Connections Pi Connections Breadboard
3,3V Photoresistor
GPIO 4 + side capacitor and Photoresistor
GND - side capacitor
import RPi.GPIO as gpio
import time

gpio.setmode(gpio.BCM)

def analogInput(pin):
    startTime = time.time()
    gpio.setup(pin, gpio.OUT)
    gpio.output(pin, gpio.LOW)
    time.sleep(0.1)  
    gpio.setup(pin, gpio.IN)
    while gpio.input(pin) == 0:
        pass
    return time.time() - startTime

while True:
    print(analogInput(4))

After executing the program you will see a number in the Terminal. If you do not see any data displayed check the connections again. The number displayed is the amount of time it took for the capacitor to be fully loaded. If you put your finger or another object on the photoresistor you should see a change in the numbers. Similarly, you should also see a change if you use your phone or flashlight and point it directly to the photoresistor. You can calculate how long it takes to fully charge a capacitor with: time=capacity * resistance. According to the formula, when the resistance is higher, it takes longer to fully charge the capacitor. Because the resistance is the only variable which can change in this circuit, we can measure the brightness in the surroundings. We measured the following value in our setup and your output should be somewhat similar: Direct lighting: 0.10 Room lighting: 0.25 Blinding with a finger >0.75

Step for Step

As usual, we begin by including the necessary libraries in lines 1 to 4 and configuring the necessary setup. In line 6 we are create our own function with the name analogInput() which needs one parameter. This parameter is called pin, and in this case refers to the gpio pin which the capacitor and the photoresistor are connected to. It is always a good idea to create your own function to make your program more readable as well as easily accessible for yourself. In line 7 we are create the variable startTime with the value of the current Unix Timestamp. This timestamp is an easy way to measure time in seconds. In the next line we set the type of the gpio pin. In beginning the pin is needed as an output pin. In line 9 we are set this output pin to low. This step is necessary so we can make sure that the capacitor is fully discharged. In line 11 are we are changing the purpose of the pin again as we then need it as an input. After this we create a while loop in line 12 that continues as long the there is a voltage reading higher than 2 volts on the pin we are measuring. The command pass in line 13 is an easy way of saying “just continue till the while loop is false”. We then calculate in line 15 the amount of time it took to discharge the capacitor. To do this we just take the measured startTime and subtract this from the current timestamp. We know that this line of code is only executed after we can’t measure anything at the input pin so we can use this to calculate the time of discharge. With the return keyword we can take a value and then return it. In the last two lines of the code we are doing nothing other than using this function in a while loop and executing it over and over again.

In conclusion, we are first discharging the capacitor, then using the program to calculate the time the capacitor needs to be charged again. This charging time depends on the resistance of the photoresistor which as we stated depends on the current brightness of its surroundings. In-turn, this is how we can measure the brightness. It is important to note that the measurement isn’t a standardized unit, but rather a value we can use for our purposes to check if it is brighter or darker in the room.

With this knowledge we could build a night light with an led which only shines when we turn off the lights. Alternatively we can also measure if we forgot to turn off the lights in our room or what really happens with the light in the refrigerator.

Noch Fragen oder Feedback?

Bevor du eine Frage stellen kannst musst du dich zuerst Anmelden oder Regestrieren!