Arduino Analog Sensor & LED Project for Beginners: Control LED Brightness
Project Overview
This beginner-friendly project demonstrates how to use an analog sensor with Arduino to control the brightness of an LED using Pulse Width Modulation (PWM). The analog sensor reads the input, and the LED brightness changes based on the sensor's output.
Project Goals for Arduino Analog Sensor & LED
- Learn how to connect an analog sensor to Arduino for input readings.
- Use PWM to control the LED brightness based on the sensor input.
- Understand how to map the analog input to an 8-bit PWM output.
Required Components for Arduino Analog Sensor & LED Project
Here’s a list of components needed to build this project:
Component | Description | Link |
---|---|---|
Arduino Uno | Main microcontroller board | Buy on Amazon |
Analog Sensor (e.g., potentiometer, soil moisture sensor) | Provides analog input | Check Availability |
LED | Outputs light based on sensor input | Buy on Amazon |
220 Ohm Resistor | Limits current to the LED | Check Availability |
Jumper Wires | Connects components to Arduino | Buy on Amazon |
Breadboard | For prototyping connections | Buy on Amazon |
What is an Analog Sensor?
An analog sensor provides a continuous voltage output that is proportional to the measured quantity. Examples include potentiometers, soil moisture sensors, and light-dependent resistors (LDRs). In this project, we will use an analog sensor to read the input, which will be used to control the LED's brightness.
Analog Sensor Pinout Overview
Pin | Description |
---|---|
VCC | Power supply (5V) |
GND | Ground connection |
OUT | Analog output, connected to Arduino analog input |
Circuit Connection for Analog Sensor & LED with Arduino
Follow these connections to set up the analog sensor and LED with Arduino:
Component | Arduino Pin | Details |
---|---|---|
Sensor VCC | 5V | Connect to 5V power supply |
Sensor GND | GND | Ground connection |
Sensor OUT | A0 | Analog output to Arduino |
LED Anode | D6 | Connects to Arduino digital pin with resistor |
LED Cathode | GND | Ground connection |
How the Circuit Works
The analog sensor reads the input voltage and outputs it to the Arduino. The analog input value is mapped to an 8-bit PWM value, which controls the LED brightness. As the sensor input increases, the LED brightness decreases, creating an inverse relationship.
Arduino Code for Analog Sensor & LED
This code reads the analog sensor's input and adjusts the LED brightness using PWM. Copy and upload this code to your Arduino Uno.
// Sensor pins: D6 for LED output, A0 for analog input
#define ledPin 6
#define sensorPin A0
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
}
void loop() {
Serial.print("Analog output: ");
Serial.println(readSensor());
delay(500);
}
// This function returns the analog data to the calling function
int readSensor() {
int sensorValue = analogRead(sensorPin); // Read the analog value from sensor
int outputValue = map(sensorValue, 0, 1023, 255, 0); // Map 10-bit data to 8-bit
analogWrite(ledPin, outputValue); // Generate PWM signal
return outputValue; // Return the mapped analog value
}
Steps to Upload Arduino Code
- Connect your Arduino to the computer using a USB cable.
- Open the Arduino IDE and paste the code into a new sketch.
- Select the correct board (e.g., Arduino Uno) and port from the "Tools" menu.
- Click the "Upload" button to transfer the code to the Arduino.
Check Output for Analog Sensor & LED
- Open the Serial Monitor (Ctrl + Shift + M) after uploading the code.
- The analog sensor reading will be displayed in the Serial Monitor.
- Adjust the sensor input (e.g., by turning a potentiometer) to change the LED brightness.
Troubleshooting Tips for Analog Sensor & LED
- No Response from Sensor: Check the wiring and ensure the sensor is connected correctly.
- LED Not Lighting Up: Verify that the sensor output is being correctly mapped to PWM output.
- Inconsistent LED Brightness: Ensure stable power supply and proper connections.
Suggestions for Beginners
Start by testing the analog sensor with the Serial Monitor to understand its behavior. Once you’re comfortable with the readings, try integrating it into more advanced sensor-based projects.
Recommended Book for Learning Arduino
Arduino Programming for Absolute Beginners - This book provides easy, step-by-step instructions, making it ideal for beginners who want to learn Arduino programming.
For more Arduino tutorials, visit MechatronicsLab.net, where you’ll find resources on Arduino, ESP8266, ESP32, and Raspberry Pi.
No comments