Understanding Automatic Street Light Using LDR (ADC) | Raspberry Pi Pico With Micropython | Part-6

SunRobotics
25 Mar 202309:20

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

00:00

πŸ’‘ 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.

05:01

πŸ”§ 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)

An ADC is a device that converts an analog signal, such as voltage, into a digital signal that a microcontroller like the Raspberry Pi Pico can read. In the video, the ADC is used to measure the voltage across an LDR (light-dependent resistor) to determine light intensity. The ADC on the Raspberry Pi Pico has three pins, and in this case, ADC 0 (GPIO 26) is used.

πŸ’‘LDR (Light Dependent Resistor)

An LDR is a type of resistor whose resistance changes based on the amount of light it is exposed to. In the video, the LDR is part of a voltage divider circuit, which helps in detecting light levels. This is used to automate the switching of an LED light based on light intensity, making it a key component in the automatic street light control project.

πŸ’‘Voltage Divider Circuit

A voltage divider is a simple circuit that divides the input voltage into smaller outputs based on the resistances in the circuit. In this project, an LDR and a 1K resistor form the voltage divider, where the output voltage is measured by the ADC pin to monitor light intensity. This configuration is essential for converting the LDR’s changing resistance into a measurable voltage.

πŸ’‘GPIO (General Purpose Input/Output)

GPIO pins are versatile pins on the Raspberry Pi Pico that can be programmed for input (receiving data) or output (sending data). In this video, GPIO 26 (ADC 0) reads the analog signal from the LDR, and GPIO 15 controls the LED. These pins allow the Raspberry Pi Pico to interface with various components in the circuit.

πŸ’‘Raspberry Pi Pico

The Raspberry Pi Pico is a microcontroller board based on the RP2040 chip, used to control electronic projects. In this video, the Pico is programmed to read light intensity from an LDR using its ADC pins and to turn an LED on or off based on the readings. It is central to automating the street light system described in the project.

πŸ’‘LED (Light Emitting Diode)

An LED is a semiconductor device that emits light when current flows through it. In this project, the LED is used to simulate an automatic street light, which turns on when the light intensity detected by the LDR drops below a certain threshold. The GPIO pin 15 on the Raspberry Pi Pico controls the LED.

πŸ’‘Python (Programming Language)

Python is the programming language used to code the logic for this project. The video uses Python to control the ADC pins and GPIO pins of the Raspberry Pi Pico. The program checks the light intensity, and if the value is below a set threshold, it activates the LED. This demonstrates how Python can be used to interact with hardware components.

πŸ’‘Thonny IDE

Thonny is an Integrated Development Environment (IDE) used to write and execute Python code, especially for microcontroller projects like this one. In the video, Thonny is used to write and upload the code to the Raspberry Pi Pico, allowing the user to program the ADC and GPIO functionalities necessary for controlling the automatic street light.

πŸ’‘Unsigned 16-bit Integer

An unsigned 16-bit integer is a data type that can represent whole numbers between 0 and 65,535. The video mentions that the ADC readings from the LDR are represented as unsigned 16-bit integers, which map the 0-3.3V range to a value between 0 and 65,535. This is essential for understanding how the light intensity is translated into a digital value.

πŸ’‘Conditional Statement (if-else)

A conditional statement is a basic programming structure that executes different actions based on whether a condition is true or false. In the video, an 'if' condition checks whether the light intensity is below a certain threshold (e.g., 2000). If true, the LED turns on, simulating the street light turning on when it's dark. If false, the LED remains off.

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

play00:00

in this video we will be discussing

play00:02

about ADC analog to digital converter in

play00:05

Raspberry Pi Pico

play00:06

we have only three analog to digital

play00:09

pins available

play00:11

this is the circuit for our project

play00:13

here there is an ldr and a resistor

play00:17

1K resistor is used here

play00:20

this series combination makes a voltage

play00:22

divider circuit

play00:24

and one terminal of the ldr is connected

play00:27

to 3.3 volts

play00:29

and the other end of the register is

play00:31

connected to ground and the meeting

play00:33

position that is coming over here

play00:35

that is going to the analog pin that is

play00:38

GPU 26 or ADC 0 is the PIN we are going

play00:41

to use this time and to control

play00:43

automatic street light by considering

play00:45

the light intensity I have added an LED

play00:48

and this led is connected to GTA 15.

play00:51

that is General pin number 20 dot this

play00:54

is a pin diagram of task Pico

play00:57

this case one is board pin number 20.

play01:00

that is GPU 50 and you can see ADC 0 is

play01:04

marked here

play01:05

that is GPA of 26. these are the pins we

play01:08

are going to use in the circuit

play01:11

let's now begin making the circuit

play01:13

for completing the circuit we need to

play01:15

resistors

play01:16

1K resistors one ldr one LED and two

play01:21

jumper wires

play01:22

let's start building it

play01:24

let me make a voltage divider circuit

play01:27

first

play01:28

just connected ldr over here I can make

play01:31

it little bit closer somewhere like this

play01:34

then from the same point I can use the

play01:37

other pin just connecting into the

play01:38

negative Trail

play01:40

now this is a common pin here in Thane

play01:42

my case and it is connected to negative

play01:44

Trails so I have to make that connection

play01:48

I'm using a jumper wire

play01:50

all the third pin that is the ground

play01:53

it is connecting to negative Trail your

play01:55

ground

play01:56

now that part is over

play01:59

next one I need to give the signal

play02:02

I wanted to take it from the common

play02:03

terminal

play02:05

this is a common 24th where the both ldr

play02:08

and the resistor are meeting

play02:10

I should go to ADC

play02:13

that is GPA pin number 26.

play02:16

let's find that that is exactly the

play02:18

position 31 the position 31 at Pin

play02:22

so you have to count it properly

play02:36

I have to give the power is there at the

play02:39

36 pin number

play02:40

I'm connecting it to the other side of

play02:43

the area

play02:44

you can follow the circuit diagram which

play02:46

is already provided

play02:53

next I need to use this led make sure

play02:56

that they are not touching

play02:58

for this I need to take a connection

play03:00

from gpo15 at

play03:02

its 20th position

play03:04

and taking action from here placing a

play03:07

system and you can use the resistor

play03:09

longest pin

play03:10

that is the positive terminal should

play03:12

connect to the terminal of the resistor

play03:15

a negative can be connected to ground

play03:18

we know that the ground this is a common

play03:20

value from here

play03:22

I can make a connection to here

play03:24

now our circuit completed

play03:27

look into the coding site for this make

play03:29

sure that you have connected your

play03:31

Raspberry Pi Pico with your laptop using

play03:33

a USB cable

play03:35

open your Tawny ID

play03:37

create a new file for using ADC

play03:41

you have to import pin

play03:43

ADC from the machine module and we need

play03:46

to use a sleep function also

play03:48

that also you have to import

play03:51

let's import those functions

play03:54

simply you can use a comma to import to

play03:57

functions from the same module

play03:59

now we have imported the needed

play04:01

functions

play04:09

we have to create two objects one is for

play04:11

the LED one is for the ADC object

play04:15

let's create an object

play04:17

for the LED there is an LED object is

play04:20

created for the GPA pin number 15.

play04:23

the same we have done in the circuit

play04:32

let's create an object for the ADC pin

play04:36

just use ADC because already we have

play04:39

imported ADC from the machine

play04:41

otherwise you have to keep machine ADC

play04:45

this is one method you can actually use

play04:47

it

play04:48

but we already imported ADC run the

play04:51

machine module we haven't imported the

play04:53

entire module

play04:54

that's why you can directly use ADC and

play04:57

here you have to mention which pin we

play04:59

are going to use

play05:01

in this case we are using GPU pin number

play05:03

26.

play05:05

that is General pin number 31.

play05:08

it is ADC 0 is the actually in the GPU

play05:11

when you are considering that is GPU 26.

play05:14

so we have created two objects

play05:17

now we have to check what is the value

play05:20

is coming then we can modify our main

play05:23

code let me upload this code

play05:25

now this time I can save into the

play05:27

computer

play05:29

I have a temporary folder in the text

play05:31

you can give a name here just save that

play05:34

so there is no error till

play05:36

now so we have created the objects right

play05:39

now

play05:40

it is very easy ADC object read normally

play05:43

in the esp32

play05:45

we use read directly it will not work in

play05:48

the case of Eco module

play05:50

you have to say that underscore u60

play05:53

now this is going to read in an integer

play05:56

having unsigned integer 16-bit

play05:59

that means it will read a value from 0

play06:01

to 65 535.

play06:05

that means it will map 0 to 3.3 volts in

play06:08

a range or in a division from 0 to 65

play06:11

535.

play06:14

let's see what is the value is going to

play06:16

show over here

play06:18

so now the light intensity is showing

play06:20

like somewhere the value is 3168.

play06:24

somewhere the value is coming now I'm

play06:27

just going to hold the sensor with my

play06:29

hand repeat the same program

play06:31

let's see what is the value is coming

play06:34

I'm getting a lower value isn't it

play06:37

now I can also use brighter light

play06:40

then you can find the values again we'll

play06:42

change that

play06:44

we will be looking right now again it's

play06:47

coming around 3000 somewhere the value

play06:50

now we are going to use that to print

play06:52

continuously and we have to get and we

play06:54

can make an automatic street light by

play06:56

measuring this value

play06:58

for that I am using while loop

play07:01

while through the data which I am taking

play07:03

from the ADC object

play07:05

I am creating a variable called Data

play07:08

you can give any variable name ADC

play07:12

ADC object read underscore steam means

play07:15

unsigned 16-bit integer which gives a

play07:18

range from 0 to 65

play07:20

535 for a voltage 0 to 3 point

play07:24

keep that in mind

play07:26

we can just print the value now I got

play07:29

the value somewhere around the normal

play07:31

lighting condition

play07:32

I am getting around the value 3000.

play07:36

the light intensity has become less than

play07:38

3000 somewhere value that is purely

play07:41

depending upon my lighting condition and

play07:43

I wanted to make the LED to be on

play07:46

for that I'm using conditional statement

play07:48

called if data

play07:50

value is supposed to be less than I am

play07:52

considering somewhere around 2000 for

play07:55

this case

play07:56

because normally my lighting conditional

play07:58

am getting 3 000. I haven't given too

play08:01

much light in my room I have to make the

play08:04

LED object to be on here

play08:06

maybe I can give a small delay 5 Seconds

play08:14

else condition in order to make the LED

play08:17

to be off

play08:26

let's run this code and see what will be

play08:28

the output

play08:30

you can see the value is printing

play08:31

somewhere around 3000 dot that is there

play08:34

isn't enough lighting condition there

play08:36

that's why the lady is not going to work

play08:39

now I am going to cover this eldia

play08:42

you can see the lighting value in

play08:44

density is less than 2 000. it is

play08:46

showing somewhere around 700 comma 800

play08:49

dot now the value is less than two

play08:51

thousand dot that's why this light is

play08:53

automatically switched on

play08:55

I remove my hand it becomes off

play09:01

that means there is enough light there

play09:03

at the street if it is going to be

play09:04

darker now you can see the light is

play09:06

going to be automatically switched on

play09:09

okay I hope this lecture is clear for

play09:12

you

play09:13

see you on the next video

play09:16

subscribe our channel for more videos

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Raspberry PiADCStreetlightLDR sensorLED circuitVoltage dividerPython codingPico projectElectronicsDIY tutorial