If you use a toggle switch, it’s enough that you just monitor the HIGH/LOW signal. But what about monitoring brightness/darkness? If you allocate “0” to the night as it’s dark and “1” to the noon as it’s bright, what would you allocate to the morning and evening? Maybe you have no solution because brightness/darkness is not digital value, which can be expressed 0/1, but analogue value. Analogue value is infinitely continuous. For example, there is 0.5 between 0 and 1, there is 0.25 between 0 and 0.5…….
But if you want to handle those analogue values with computer, what do you have to do? The answer is that you need to convert them to digital values. The goal of today is to learn how to convert analogue value into digital value. To achieve this goal, I will make a LED system that is turned on and off according to the brightness in my room.
|Table of contents|
What you need
You may want a nipper to cut the legs of resister if they are long.
|Red LED||1||Vf = 2.0 V|
|Breadboard jumper wire||5||male-female|
|Breadboard jumper wire||7||male-male|
1-1. AD converter
To handle digital value with computer, you need to convert it into digital value. As its name tells, AD converter converts analogue value into digital value. I use MCP3208-CI/P, a 12 bit AD converter. It’s 12 bit – 0 to 4095. When it reads 0 V (analogue), it converts into 0 (digital), and when it reads 3.3 V, it converts into 4095. AD converter apply a digital value to any given analogue value. For example, 1.6666666666666…… and 1.6666666666665 are converted into the same analogue value. So it’s like you scale some object with a ruler and know its length. You may say its 1.5 cm but it could be 1.51 cm or 1.56 cm, but that precision is not necessary for you!
AD converter changes the decimal (0-4095) digit to binary digit to send it to Raspberry Pi. Then Pi can receive 0-4095 as binary value.
Photoresister is a resister the resistance of which changes depending on surrounding luminance. Its resistance often varies from 10 MΩ in the dark to 10 kΩ in the light. By using this property, you can make LED turn on/off in accordance with surrounding luminance.
2. Hardware setup
Here is the circuit diagram. The voltage of node between the photoresister and the resister (10 kΩ) get s higher when the photoresister is in the light. On the other hand, it gets lower when photoresister is in the dark. The node is connected with CH0 of MCP3208 and the node voltage is read through the CH0.
As the input voltage changes depending on the surrounding luminance, Thus, By making use of this variance, you can control GPIO 25 to output 3.3V and control LED.
Here is the setup that I made.
Here is the code. The function “readadc” for the ADC converter to function. Please note that this is ready-made code and I cannot explain what it is in details. What I’d like to mention here is line 52. In my environment, it’s around 2700 when my room is light while it’s around 300 when my room is dark. Thus, I had to set the value somewhere between 300 and 2700. I set 2000 for the LED to light up when the voltage is lower than 2000. So please change the value in line 52 for your environment.
import RPi.GPIO as GPIO
from time import sleep
# Get 12bit digital value from MCP3208 with SPI. Available CH0 to CH7.
def readadc(adcnum, clockpin, mosipin, misopin, cspin):
if adcnum > 7 or adcnum < 0:
commandout = adcnum
commandout |= 0x18 # start bit + single ended bit
commandout <<= 3 # send 8th bit from LSB
for i in range(5):
# send 8th to 4th bit from LSB
if commandout & 0x80:
commandout <<= 1
adcout = 0
# Read 13 bits (null bit + 12 bit data)
for i in range(13):
adcout <<= 1
if i>0 and GPIO.input(misopin)==GPIO.HIGH:
adcout |= 0x1
# Define pin names as valuables
SPICLK = 11
SPIMOSI = 10
SPIMISO = 9
SPICS = 8
# Define IN/OUTPUT for SPI communication
LED = 25
inputVal0 = readadc(0, SPICLK, SPIMOSI, SPIMISO, SPICS)
if inputVal0 < 2000:
As you can see below, the LED is turned on/off when my room light is turned on/off. Also, you can see the voltage value on the screen, which is realized by line 56 “print(inputVal0)”. Thus by using a AD converter, analogue value–brightness/darkness–is converted into digital value. In that sense, the value in the line 52 is your digital definition of where it’s light or dark.