Raspberry Pi of Goemon

Raspberry Pi3 learning blog

Beginner Electric work

AD converter: LED responsive to room brightness

Post:14/10/2016 Update:

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

  1. Parts
  2. Hardware setup
  3. Code
  4. Result
  5. Reference

What you need

You may want a nipper to cut the legs of resister if they are long.

Name Qty
Resistor 1 330 Ω
Resistor 1 10~100 kΩ
Red LED 1 Vf = 2.0 V
Breadboard 1
Breadboard jumper wire 5  male-female
Breadboard jumper wire 7 male-male
AD converter 1  MCP3208-CI/P
Photoresistor 1
Nipper 1
Sponsored Link

1. Parts


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.


1-2. Photoresister

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.



3. Code

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.

4. Result

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.

img_20161011_193239 img_20161011_193245 img_20161011_193252 img_20161011_193259


5. Reference

  1. https://goo.gl/Pli6jQ
  2. http://www.mouser.com/ProductDetail/Microchip-Technology/MCP3208-CI-P/?qs=9y3LFqDLL8IuAGJEebQX9g%3d%3d
Sponsored Link

Sponsored Link

-Beginner, Electric work

Related Posts


How to deal with switch bounce

Today, I will deal with switch bounce. Then I will toggle the switch to control LED (OFF/ON). Now let’s start! Table of contents Switch bounce Bounce time Code Result Reference Sponsored Link 1. Switch bounce Switch bounce is a phenomenon that occurs when you use a physical contact switch such as a tact switch. When you push a switch, you may think it gets immediately ON. However, that’s not correct in the micro-scale world. When a switch is toggled, metal contacts of a switch come together and close. During this time, metal contacts are bouncing and that causes noise. This …


Blink LED on Raspberry Pi 3

Blink LED is a very first step for beginners. You can learn how to connect each electric component and control it with minimum numbers of components and a small chunk of code. Table of contents GPIO pin layout Hardware setup Code Result Reference The necessary parts and tools are here. The selection of resistor depends on LED’s Vf (forward voltage). You can usually find the value on its specification/catalog. Name Qty Resistor 1 330 Ω Red LED 1 Vf = 2.0 V Breadboard 1 Breadboard jumper wire (male-female) 2 Nippers 1 Sponsored Link 1. GPIO pin layout GPIO (General Purpose Input/Output) …


How to use your tablet/smartphone as screen

Raspberry Pi is small and portable in itself. Also you can easily carry a charger for it. However, you are unlikely to carry a screen with you. It’ll be super convenient if you can use your smartphone/tablet as screen for Raspberry Pi. Today is about how to make your tablet/smartphone screen for Raspberry Pi. Please note that this method is possible only when both of your mobile devices and Raspberry Pi are connected to a same network. Table of contents Check Raspberry Pi IP address Set up SSH Install & set up VNC server on Raspberry Pi Install & set up …


Utilize an internal pull-down resister

In the previous post, I checked how a pull-down resister functioned. When it’s there the system worked as expected. However once it’s taken off, then LED became unstable. Actually, Raspberry Pi 3 contains an internal pull-down resister and you can enable it by coding. Today, I’m gonna show you how you can enable an internal resister and how it works. Don’t worry, it’s very simple 🙂


Control LED by tact switch

In a previous post, how to blink LED with Raspberry Pi 3 was introduced (Blink LED on Raspberry Pi 3 ). In this work, I just got an “output” from Raspberry Pi. Today, I will send an “input” to Raspberry Pi by using a tact switch.By sending an input, you can get Raspberry Pi to send an output. With this work, you can turn on/off your LED with a tact switch. Table of contents Tact switch Hardware setup Code Result Reference What you need You may want a nipper to cut the legs of resister if they are long. Name Qty Resistor …