Arduino Multi-LED Sensor Project for Beginners: Visualize Sensor Readings

Arduino Multi-LED Sensor Project for Beginners: Visualize Sensor Readings

Project Overview

This beginner-friendly project demonstrates how to use multiple LEDs with Arduino to visualize analog sensor readings. The LEDs turn on or off based on the sensor's value, giving a simple visual indication of the sensor's state.

Project Goals for Arduino Multi-LED Sensor

  • Learn how to connect multiple LEDs and analog sensors to Arduino for real-time visualization.
  • Understand how to read and interpret analog sensor values.
  • Control LEDs based on sensor input thresholds.

Required Components for Arduino Multi-LED Sensor Project

Here’s a list of components needed to build this project:

ComponentDescriptionLink
Arduino UnoMain microcontroller boardBuy on Amazon
Analog Sensor (e.g., potentiometer, soil moisture sensor)Provides analog inputCheck Availability
LEDs (5)Indicates sensor readingsBuy on Amazon
220 Ohm Resistors (5)Limits current to each LEDCheck Availability
Jumper WiresConnects components to ArduinoBuy on Amazon
BreadboardFor prototyping connectionsBuy on Amazon

What is an Analog Sensor?

An analog sensor provides a continuous voltage output that varies according to the measured quantity. Examples include potentiometers, soil moisture sensors, and temperature sensors. In this project, we will use an analog sensor to read input values and trigger different LEDs based on predefined thresholds.

Circuit Connection for Multi-LED & Analog Sensor with Arduino

Follow these connections to set up the multi-LED circuit and analog sensor with Arduino:

ComponentArduino PinDetails
Sensor VCC5VConnect to 5V power supply
Sensor GNDGNDGround connection
Sensor OUTA0Analog output to Arduino
LED 1 AnodeD2Connects to Arduino digital pin with resistor
LED 2 AnodeD3Connects to Arduino digital pin with resistor
LED 3 AnodeD4Connects to Arduino digital pin with resistor
LED 4 AnodeD5Connects to Arduino digital pin with resistor
LED 5 AnodeD6Connects to Arduino digital pin with resistor
LED CathodesGNDConnects to ground

How the Circuit Works

The analog sensor reads the input voltage and outputs it to the Arduino. Depending on the sensor value, different LEDs are turned on or off to represent various thresholds. This provides a simple visual representation of the sensor's state.

Arduino Code for Multi-LED Sensor

This code reads the analog sensor's input and controls the LEDs based on different sensor value ranges. Copy and upload this code to your Arduino Uno.


/* Arduino pins where the LEDs are attached */
#define LED_1 2
#define LED_2 3
#define LED_3 4
#define LED_4 5
#define LED_5 6

#define sensorPin A0 // Analog input pin for the sensor

/* Boolean variables to hold the status of the LEDs */
bool ledPin1Status;
bool ledPin2Status;
bool ledPin3Status;
bool ledPin4Status;
bool ledPin5Status;

void setup() {
  pinMode(LED_1, OUTPUT);
  pinMode(LED_2, OUTPUT);
  pinMode(LED_3, OUTPUT);
  pinMode(LED_4, OUTPUT);
  pinMode(LED_5, OUTPUT);
  pinMode(sensorPin, INPUT);

  Serial.begin(9600); // Initialize serial communications at 9600 bps
}

void loop() {
  int sensorValue = analogRead(sensorPin);
  Serial.println(sensorValue);

  if (sensorValue > 555) ledPin1Status = 1;
  if (sensorValue > 558) ledPin2Status = 1;
  if (sensorValue > 560) ledPin3Status = 1;
  if (sensorValue > 562) ledPin4Status = 1;
  if (sensorValue > 564) ledPin5Status = 1;

  if (ledPin1Status || ledPin2Status || ledPin3Status || ledPin4Status || ledPin5Status) {
    if (sensorValue > 555 || sensorValue < 537) digitalWrite(LED_1, HIGH);
    if (sensorValue > 558 || sensorValue < 534) digitalWrite(LED_2, HIGH);
    if (sensorValue > 560 || sensorValue < 534) digitalWrite(LED_3, HIGH);
    if (sensorValue > 562 || sensorValue < 531) digitalWrite(LED_4, HIGH);
    if (sensorValue > 564 || sensorValue < 528) digitalWrite(LED_5, HIGH);

    delay(200);

    ledPin5Status = 0;
    ledPin4Status = 0;
    ledPin3Status = 0;
    ledPin2Status = 0;
    ledPin1Status = 0;
  }

  digitalWrite(LED_1, LOW);
  digitalWrite(LED_2, LOW);
  digitalWrite(LED_3, LOW);
  digitalWrite(LED_4, LOW);
  digitalWrite(LED_5, LOW);
}

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 Multi-LED Sensor

  • Open the Serial Monitor (Ctrl + Shift + M) after uploading the code.
  • The sensor readings will be displayed in the Serial Monitor.
  • As the sensor value changes, different LEDs will turn on or off based on the defined thresholds.

Troubleshooting Tips for Multi-LED Sensor

  • No Response from Sensor: Check the wiring and ensure the sensor is connected correctly.
  • LEDs Not Lighting Up: Verify that the sensor output exceeds the set threshold for each LED.
  • Inconsistent LED Behavior: 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, adjust the threshold values for the LEDs to better suit your project needs.

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

Theme images by Dizzo. Powered by Blogger.