
Inputs and outputs: LDR sensor
Turn a led on when ambient light is low
What are we going to do?
In this activity you'll write some code that turn a led on o off when ambient light is low. We will achieve this thanks to a LDR sensor brick or LDR circuit.
You can use the code from Arduino IDE -> File -> Examples -> Digital -> Button but you'll need to change some parts.
UNO board
You can use any Arduino UNO original or compatible board
USB cable
Depending on your board, you'll need a microUSB cable or a miniUSB cable
Computer
You need a computer with Arduino IDE software. If you don't have it, follow this link with instructions to download and install it.
LDR sensor brick
A simple LDR sensor brick with SVG connections
LED brick
A monocolor LED brick with integrated resistors and SVG connections
Let's get started
Follow these instructions carefully. You can click on some of the images to zoom in.
Always connect the components BEFORE powering the UNO board or YOU MAY DAMAGE YOUR BOARD OR COMPONENTS.
Instructions
2. Open Arduino IDE
Once you've opened the editor, open the button example from File->Examples->Digital->Button
3. Change code
We'll need to modify the button example to make it more suitable to our circuit.
- Change buttonPin variable name for ldrSensorPin on all the variable references on your code
- Change ledPin = 13 variable value (which is 13 on the example) to suit your connections
- Change buttonState variable name for sensorState on all the variable references on your code
4. Upload to the board
Use the upload button to transfer the program to your board. Once done, you should read Upload completed. That means the transfer was succesful. If it's not succesful, try disconnecting your board and connecting it again and repeating the upload process.
5. Final result
After uploading, everything should be working as planned. Otherwise, double check connections and code.
Congratulations, you've done it! You're on the right path to be a maker! 🙂
Now, what else?
Now is your turn:
- Think about how would you modify the code so the sensor acts differently depending on the ambient light.
- Do you have any real life application for this kind of circuit?
Pingback: Temp and hum sensor – makit
Pingback: Seven segment display – makit
Pingback: Encoder and servo – makit
Pingback: PIR sensor – makit
Pingback: Buzzer! – makit