IotBeginner

Digital Sensor LED Control

Control an LED using digital sensor input and Arduino digital signal processing.

Technologies Used

Arduino UnoDigital SensorLEDResistor

Digital Sensor-Based LED Control System

1. Introduction

This Arduino program reads a digital signal from a sensor connected to digital pin 7.

When the sensor output is HIGH, the LED connected to pin 13 turns ON. When the sensor output is LOW, the LED turns OFF.

The sensor type is not specified in the code. Therefore, this project uses a general digital-output sensor. It could be a motion sensor, obstacle sensor, or another module that provides HIGH/LOW output.

2. Objectives

  • Understand how Arduino reads a digital sensor.
  • Learn the use of digitalRead() and digitalWrite().
  • Control an LED according to a sensor's output.
  • Display the sensor value in the Serial Monitor.

3. Required Components

ComponentQuantityPurpose
Arduino Uno or compatible board1Runs the program
Digital-output sensor module1Sends HIGH or LOW signal
LED1Shows the sensor state
220Ω–330Ω resistor1Limits LED current
Breadboard1Makes connections easier
Jumper wiresAs neededConnect components
USB cable1Uploads code and powers the board

Note: Arduino Uno's built-in LED is connected to pin 13 on many boards. If using the built-in LED, an external LED and resistor are not required.

4. Pin Connections

Arduino PinConnect To
Digital Pin 7Sensor digital output (OUT)
5V or suitable supplySensor VCC, according to its specification
GNDSensor GND
Digital Pin 13LED (built-in LED or external LED circuit)

External LED Connection

  • Connect Arduino pin 13 to the LED anode through a 220Ω–330Ω resistor.
  • Connect the LED cathode to GND.
  • Check the sensor module's voltage requirements before connecting it.

5. Important Concepts

5.1 pinMode()

Sets whether a pin is used as an input or output.

pinMode(ledPin, OUTPUT); pinMode(sensorPin, INPUT);
  • OUTPUT: Arduino sends a signal to control the LED.
  • INPUT: Arduino reads the signal from the sensor.

5.2 digitalRead()

Reads the digital state of a pin.

val = digitalRead(sensorPin);

It returns one of two states:

  • HIGH: A HIGH logic signal is detected.
  • LOW: A LOW logic signal is detected.

5.3 digitalWrite()

Sets a digital output pin to HIGH or LOW.

digitalWrite(ledPin, HIGH); // LED ON digitalWrite(ledPin, LOW); // LED OFF

5.4 Boolean Variable

boolean val = 0;

A Boolean variable stores either true or false.

In Arduino code:

  • 0 represents false.
  • 1 represents true.

The variable val stores the sensor's digital reading.

5.5 Serial Communication

Serial.begin(9600); Serial.println(val);
  • Serial.begin(9600) starts serial communication at 9600 baud.
  • Serial.println(val) prints the sensor reading in the Serial Monitor, usually as 0 or 1.

6. Complete Arduino Code

int ledPin = 13; int sensorPin = 7; boolean val = 0; void setup() { pinMode(ledPin, OUTPUT); pinMode(sensorPin, INPUT); Serial.begin(9600); } void loop() { val = digitalRead(sensorPin); Serial.println(val); // When the sensor output is HIGH, the LED turns ON. if (val == HIGH) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); } }

This is the supplied program, formatted for readability. The code checks for a digital HIGH signal; it does not measure an analog threshold.

7. Line-by-Line Explanation

CodeExplanation
int ledPin = 13;Stores the LED pin number.
int sensorPin = 7;Stores the sensor input pin number.
boolean val = 0;Creates a Boolean variable for the sensor reading.
void setup()Runs once when the Arduino starts or resets.
pinMode(ledPin, OUTPUT);Sets pin 13 as an output.
pinMode(sensorPin, INPUT);Sets pin 7 as an input.
Serial.begin(9600);Starts serial communication.
void loop()Repeats continuously.
digitalRead(sensorPin)Reads the sensor's digital output.
Serial.println(val);Prints the reading in the Serial Monitor.
if (val == HIGH)Checks whether the sensor reading is HIGH.
digitalWrite(ledPin, HIGH);Turns the LED ON.
elseRuns when the reading is LOW.
digitalWrite(ledPin, LOW);Turns the LED OFF.

8. Working Process

  1. Arduino starts and runs setup().
  2. Pin 13 is configured as an output.
  3. Pin 7 is configured as an input.
  4. Serial communication starts at 9600 baud.
  5. The loop() function reads the sensor output.
  6. The reading is printed in the Serial Monitor.
  7. If the reading is HIGH, the LED turns ON.
  8. If the reading is LOW, the LED turns OFF.
  9. These steps repeat continuously.

9. Logic Table

Sensor OutputvalLED State
HIGH1ON
LOW0OFF

10. Block Diagram

Digital Sensor | | OUT v Arduino Digital Pin 7 | Read HIGH / LOW | +-----+-----+ | | HIGH LOW | | v v LED ON LED OFF Pin 13 Pin 13 | v Serial Monitor (prints 1 or 0)

11. Program Flowchart

START | v Set LED pin as OUTPUT Set sensor pin as INPUT Start Serial at 9600 | v Read sensor on pin 7 | v Print sensor value | v Is value HIGH? / \ YES NO | | v v Turn LED Turn LED ON OFF \ / \ / v Repeat loop()

12. Expected Serial Monitor Output

When the sensor output is LOW, the Serial Monitor displays:

0 0 0 0

When the sensor output becomes HIGH, it displays:

1 1 1 1

The exact sequence depends on the sensor and its environment.

Open the Arduino IDE Serial Monitor and select 9600 baud to view the output.

13. Applications

  • Motion indication when used with a digital-output motion sensor.
  • Obstacle detection when used with a digital obstacle sensor.
  • Object-presence indication.
  • Simple automatic LED control.
  • Learning digital input and output in Arduino projects.

The actual application depends on the sensor module connected to pin 7.

14. Common Errors and Troubleshooting

ProblemPossible ReasonSolution
LED never turns ONSensor output never becomes HIGHCheck sensor operation and wiring.
LED stays ONSensor output remains HIGHCheck sensor settings and environment.
Serial Monitor shows unexpected valuesWrong baud rateSelect 9600 baud.
Sensor reading changes randomlyInput may be floating or wiring is looseCheck the module's output and connections.
Arduino does not detect the sensorIncorrect pin connection or powerVerify OUT, VCC, and GND connections.
External LED does not glowLED polarity or wiring is incorrectCheck anode, cathode, and resistor connection.

Input Note: If the sensor has an open-collector/open-drain output or requires a pull-up, follow its datasheet. In some circuits, INPUT_PULLUP may be appropriate, but the logic can then be inverted. Do not change INPUT to INPUT_PULLUP without checking the sensor's output circuit.

15. Precautions

  • Connect sensor VCC and GND correctly.
  • Check the sensor's operating voltage before connecting it to Arduino.
  • Use a current-limiting resistor with an external LED.
  • Do not connect an LED directly across a power supply without suitable current limiting.
  • Keep wires secure and avoid short circuits.
  • Remember that HIGH and LOW behavior depends on the sensor module's output design.

16. Viva Questions and Answers

Q1. What is the purpose of this program?

It reads a digital sensor signal and controls an LED based on that signal.

Q2. Which pin is used for the LED?

Digital pin 13.

Q3. Which pin is used for the sensor?

Digital pin 7.

Q4. What is pinMode() used for?

It configures a pin as an input or output.

Q5. What is digitalRead()?

It reads the HIGH or LOW state of a digital input pin.

Q6. What is digitalWrite()?

It sets a digital output pin to HIGH or LOW.

Q7. What does HIGH mean in this program?

The sensor input is at a HIGH logic level, so the LED turns ON.

Q8. What does LOW mean in this program?

The sensor input is at a LOW logic level, so the LED turns OFF.

Q9. Why is Serial.begin(9600) used?

It starts serial communication at 9600 baud.

Q10. Why is Serial.println(val) used?

It displays the sensor reading in the Serial Monitor.

Q11. What is a Boolean variable?

A variable that stores either true or false.

Q12. How many times does loop() execute?

It repeats continuously while the Arduino is running.

Q13. What is the difference between HIGH and LOW?

They represent the two digital logic states.

Q14. Does this code read an analog value?

No. It uses digitalRead(), which reads a digital HIGH/LOW signal.

Q15. Does this code use a threshold value?

No. It checks whether the digital input is HIGH or LOW. No numeric threshold is defined.

17. Conclusion

This Arduino project demonstrates basic digital sensor interfacing.

Arduino reads the sensor's output from pin 7 using digitalRead(). If the output is HIGH, it turns ON the LED on pin 13; otherwise, it turns the LED OFF.

The program also prints the sensor state in the Serial Monitor. It is a useful beginner exercise for understanding digital inputs, digital outputs, conditional statements, and serial communication.

18. Document Information

ItemDetails
Document titleArduino Digital Sensor with LED Control
Programming languageArduino C/C++
Board exampleArduino Uno
Sensor connectionDigital Pin 7
LED connectionDigital Pin 13
Serial baud rate9600
File formatMarkdown (.md)