Understanding Automatic Street Light Using LDR (ADC) | Raspberry Pi Pico With Micropython | Part-6
Summary
TLDRIn this video, the process of setting up an automatic street light using a Raspberry Pi Pico and an LDR (light-dependent resistor) is demonstrated. The circuit uses a voltage divider with the LDR and a 1K resistor, and the Raspberry Pi's analog-to-digital pins to monitor light intensity. An LED is connected to indicate when the light level drops below a set threshold, automatically turning on the street light. The video also covers the necessary coding in MicroPython using Thonny IDE, object creation for the LED and ADC, and setting conditions for controlling the LED based on light intensity readings.
Takeaways
- 🔌 The video discusses using an ADC (Analog to Digital Converter) with the Raspberry Pi Pico, which has only three ADC pins.
- 🛠️ The project involves creating an automatic street light by using an LDR (Light Dependent Resistor) and a 1K resistor in a voltage divider circuit.
- ⚡ The LDR is connected to 3.3V and the resistor is connected to ground. The midpoint of this circuit is connected to ADC pin GPIO 26 (ADC 0).
- 💡 An LED is added to the circuit, connected to GPIO pin 15 (General pin number 20), to control light intensity.
- 🖥️ To complete the circuit, two resistors, an LDR, an LED, and jumper wires are needed. The LDR-resistor combination forms the voltage divider.
- 📂 On the software side, the Raspberry Pi Pico is connected to a laptop via USB, and code is written using the Thonny IDE.
- 💻 The Python code imports the necessary modules (Pin, ADC) from the machine module and creates objects for both the LED and the ADC.
- 🔢 The ADC reads an unsigned 16-bit integer value ranging from 0 to 65,535, which corresponds to 0 to 3.3V from the LDR circuit.
- 🔄 The program uses a while loop and conditional statements to control the LED based on the light intensity value from the ADC.
- 🌃 The circuit can automatically switch the LED on or off based on light intensity, simulating an automatic street light functionality.
Q & A
What is the primary component being discussed in this video?
-The primary component being discussed is the ADC (Analog to Digital Converter) in Raspberry Pi Pico.
How many analog-to-digital converter pins are available on the Raspberry Pi Pico?
-There are three analog-to-digital converter (ADC) pins available on the Raspberry Pi Pico.
What components are used to create the voltage divider circuit in this project?
-An LDR (Light Dependent Resistor) and a 1K resistor are used to create the voltage divider circuit.
How is the voltage divider circuit connected to the Raspberry Pi Pico?
-One terminal of the LDR is connected to 3.3 volts, the other end of the resistor is connected to ground, and the meeting point between the LDR and resistor is connected to GPIO pin 26 (ADC0) on the Raspberry Pi Pico.
What is the function of the LED in this project?
-The LED is used to indicate when the light intensity is below a certain threshold, simulating an automatic street light system.
Which pin is the LED connected to in the Raspberry Pi Pico?
-The LED is connected to GPIO pin 15 (pin 20 on the board).
What software is used to write and upload the code to the Raspberry Pi Pico?
-The Thonny IDE is used to write and upload the code to the Raspberry Pi Pico.
What Python functions are imported to control the ADC and the LED?
-The 'Pin' and 'ADC' functions are imported from the 'machine' module, and the 'sleep' function is imported to control timing.
How is the analog value read from the LDR converted into a digital value?
-The ADC reads the analog value from the LDR and converts it into a 16-bit unsigned integer ranging from 0 to 65,535, which corresponds to 0 to 3.3 volts.
How does the code determine when to turn the LED on or off?
-The code uses a conditional statement that checks if the light intensity value (read by the ADC) is less than 2000. If it is, the LED is turned on, otherwise, it remains off.
Outlines
💡 Introduction to ADC on Raspberry Pi Pico
The video introduces the concept of an Analog to Digital Converter (ADC) on the Raspberry Pi Pico, which has three ADC pins. It explains the setup of a circuit that uses an LDR (Light Dependent Resistor) and a 1K resistor to form a voltage divider circuit. The LDR is connected to 3.3 volts, and the resistor is grounded. The midpoint of the voltage divider is connected to pin GP26 (ADC0) of the Raspberry Pi Pico. An LED is connected to GP15, and the objective is to control the streetlight automatically by sensing light intensity.
🔧 Circuit Assembly and Pin Connections
The speaker details the assembly of the circuit, including the required components: two resistors, one LDR, one LED, and two jumper wires. The steps involve connecting the LDR and resistor in a voltage divider configuration and making the necessary connections to the Raspberry Pi Pico pins (GP26 for the analog input and GP15 for the LED control). The video highlights the importance of following the provided circuit diagram and ensuring connections to power, ground, and signal pins for proper operation.
🖥️ Coding for ADC and LED Control
The video moves on to coding, demonstrating how to connect the Raspberry Pi Pico to a computer via USB and open the Thonny IDE. It explains importing the necessary modules (`Pin`, `ADC`, and `sleep`) from the machine module and shows how to create objects for controlling the LED and reading data from the ADC pin. The code reads values from the ADC pin (GP26) and checks the light intensity, preparing the system for automatic streetlight control based on sensor readings.
📝 Writing the ADC Reading Logic
The coding process continues with the explanation of reading values from the ADC. The ADC object reads a 16-bit unsigned integer (0 to 65,535) representing a voltage range from 0 to 3.3V. The speaker checks the light intensity by printing the values from the sensor and demonstrates how different light conditions (covering the sensor, exposing it to bright light) affect the ADC readings, which range from around 3000 in normal light to lower values when obstructed.
⚙️ Implementing the Automatic Streetlight Logic
In this section, the video introduces a while loop that continuously reads the light intensity from the ADC. A variable (`data`) is used to store the ADC values, and an if-else statement checks if the value drops below 2000 (indicating low light). If the light intensity is low, the LED turns on, simulating the streetlight turning on automatically. The code also includes a small delay and an else condition to turn off the LED when there is sufficient light.
🔄 Testing the Code and Demonstrating Results
The video concludes with a demonstration of the code in action. The ADC values are printed in real-time, and the LED turns on or off depending on the light intensity. The speaker shows that when the light intensity is less than 2000 (when the sensor is covered), the LED turns on, mimicking an automatic streetlight turning on during darkness. Removing the obstruction or increasing the light causes the LED to turn off, showing that the streetlight is not needed.
📢 Conclusion and Next Steps
The video wraps up by summarizing the project, noting that the automatic streetlight system works by using an ADC to measure light intensity. The video encourages viewers to subscribe to the channel for more tutorials and hints at future projects involving Raspberry Pi Pico and similar components.
Mindmap
Keywords
💡ADC (Analog to Digital Converter)
💡LDR (Light Dependent Resistor)
💡Voltage Divider Circuit
💡GPIO (General Purpose Input/Output)
💡Raspberry Pi Pico
💡LED (Light Emitting Diode)
💡Python (Programming Language)
💡Thonny IDE
💡Unsigned 16-bit Integer
💡Conditional Statement (if-else)
Highlights
Introduction to ADC (Analog to Digital Converter) in Raspberry Pi Pico.
Raspberry Pi Pico has three ADC pins; in this project, ADC 0 (GPU pin 26) is used.
An LDR (Light Dependent Resistor) and a 1K resistor form a voltage divider circuit for measuring light intensity.
The LDR is connected to 3.3V and the resistor is connected to ground; the meeting point goes to ADC 0.
The project aims to control an automatic streetlight based on light intensity.
An LED is used to represent the streetlight, connected to GPIO pin 15.
Required components for the circuit: two 1K resistors, one LDR, one LED, and two jumper wires.
Building the voltage divider circuit step-by-step with the LDR and resistor.
Using a jumper wire to connect the common pin to the ground in the circuit.
The analog signal from the voltage divider is sent to ADC pin 26 (GPIO pin 31).
Coding the project: Importing the necessary libraries from the machine module (Pin, ADC) and sleep function.
Creating objects for the LED and ADC to control GPIO pin 15 and read data from ADC pin 26.
The ADC object reads values from 0 to 65,535, mapping the range of 0 to 3.3V.
Using a while loop to continuously read light intensity values and control the LED.
Conditional logic: The LED turns on when the light intensity falls below a certain threshold (e.g., 2000).
Demonstrating the automatic streetlight by covering the LDR and observing the LED behavior.
Transcripts
in this video we will be discussing
about ADC analog to digital converter in
Raspberry Pi Pico
we have only three analog to digital
pins available
this is the circuit for our project
here there is an ldr and a resistor
1K resistor is used here
this series combination makes a voltage
divider circuit
and one terminal of the ldr is connected
to 3.3 volts
and the other end of the register is
connected to ground and the meeting
position that is coming over here
that is going to the analog pin that is
GPU 26 or ADC 0 is the PIN we are going
to use this time and to control
automatic street light by considering
the light intensity I have added an LED
and this led is connected to GTA 15.
that is General pin number 20 dot this
is a pin diagram of task Pico
this case one is board pin number 20.
that is GPU 50 and you can see ADC 0 is
marked here
that is GPA of 26. these are the pins we
are going to use in the circuit
let's now begin making the circuit
for completing the circuit we need to
resistors
1K resistors one ldr one LED and two
jumper wires
let's start building it
let me make a voltage divider circuit
first
just connected ldr over here I can make
it little bit closer somewhere like this
then from the same point I can use the
other pin just connecting into the
negative Trail
now this is a common pin here in Thane
my case and it is connected to negative
Trails so I have to make that connection
I'm using a jumper wire
all the third pin that is the ground
it is connecting to negative Trail your
ground
now that part is over
next one I need to give the signal
I wanted to take it from the common
terminal
this is a common 24th where the both ldr
and the resistor are meeting
I should go to ADC
that is GPA pin number 26.
let's find that that is exactly the
position 31 the position 31 at Pin
so you have to count it properly
I have to give the power is there at the
36 pin number
I'm connecting it to the other side of
the area
you can follow the circuit diagram which
is already provided
next I need to use this led make sure
that they are not touching
for this I need to take a connection
from gpo15 at
its 20th position
and taking action from here placing a
system and you can use the resistor
longest pin
that is the positive terminal should
connect to the terminal of the resistor
a negative can be connected to ground
we know that the ground this is a common
value from here
I can make a connection to here
now our circuit completed
look into the coding site for this make
sure that you have connected your
Raspberry Pi Pico with your laptop using
a USB cable
open your Tawny ID
create a new file for using ADC
you have to import pin
ADC from the machine module and we need
to use a sleep function also
that also you have to import
let's import those functions
simply you can use a comma to import to
functions from the same module
now we have imported the needed
functions
we have to create two objects one is for
the LED one is for the ADC object
let's create an object
for the LED there is an LED object is
created for the GPA pin number 15.
the same we have done in the circuit
let's create an object for the ADC pin
just use ADC because already we have
imported ADC from the machine
otherwise you have to keep machine ADC
this is one method you can actually use
it
but we already imported ADC run the
machine module we haven't imported the
entire module
that's why you can directly use ADC and
here you have to mention which pin we
are going to use
in this case we are using GPU pin number
26.
that is General pin number 31.
it is ADC 0 is the actually in the GPU
when you are considering that is GPU 26.
so we have created two objects
now we have to check what is the value
is coming then we can modify our main
code let me upload this code
now this time I can save into the
computer
I have a temporary folder in the text
you can give a name here just save that
so there is no error till
now so we have created the objects right
now
it is very easy ADC object read normally
in the esp32
we use read directly it will not work in
the case of Eco module
you have to say that underscore u60
now this is going to read in an integer
having unsigned integer 16-bit
that means it will read a value from 0
to 65 535.
that means it will map 0 to 3.3 volts in
a range or in a division from 0 to 65
535.
let's see what is the value is going to
show over here
so now the light intensity is showing
like somewhere the value is 3168.
somewhere the value is coming now I'm
just going to hold the sensor with my
hand repeat the same program
let's see what is the value is coming
I'm getting a lower value isn't it
now I can also use brighter light
then you can find the values again we'll
change that
we will be looking right now again it's
coming around 3000 somewhere the value
now we are going to use that to print
continuously and we have to get and we
can make an automatic street light by
measuring this value
for that I am using while loop
while through the data which I am taking
from the ADC object
I am creating a variable called Data
you can give any variable name ADC
ADC object read underscore steam means
unsigned 16-bit integer which gives a
range from 0 to 65
535 for a voltage 0 to 3 point
keep that in mind
we can just print the value now I got
the value somewhere around the normal
lighting condition
I am getting around the value 3000.
the light intensity has become less than
3000 somewhere value that is purely
depending upon my lighting condition and
I wanted to make the LED to be on
for that I'm using conditional statement
called if data
value is supposed to be less than I am
considering somewhere around 2000 for
this case
because normally my lighting conditional
am getting 3 000. I haven't given too
much light in my room I have to make the
LED object to be on here
maybe I can give a small delay 5 Seconds
else condition in order to make the LED
to be off
let's run this code and see what will be
the output
you can see the value is printing
somewhere around 3000 dot that is there
isn't enough lighting condition there
that's why the lady is not going to work
now I am going to cover this eldia
you can see the lighting value in
density is less than 2 000. it is
showing somewhere around 700 comma 800
dot now the value is less than two
thousand dot that's why this light is
automatically switched on
I remove my hand it becomes off
that means there is enough light there
at the street if it is going to be
darker now you can see the light is
going to be automatically switched on
okay I hope this lecture is clear for
you
see you on the next video
subscribe our channel for more videos
Ver Más Videos Relacionados
5.0 / 5 (0 votes)