Tilt and Glow: Dynamic LED Control With a Tilt Sensor

by adiyenuubarii in Circuits > Arduino

47 Views, 0 Favorites, 0 Comments

Tilt and Glow: Dynamic LED Control With a Tilt Sensor

WhatsApp Image 2024-06-30 at 19.05.36.jpeg

Tilt and Glow is a project I developed while learning the basics of using sensors and output devices with Arduino. I wanted to create a circuit that could switch between LEDs using a sensor and also enable me to adjust their brightness. It was a rewarding experience exploring these functionalities and integrating them into a practical application.

By following the below-mentioned steps, you can effectively implement and understand the code for controlling LEDs based on tilt and adjusting brightness using a potentiometer with Arduino. Adjustments and enhancements can be made based on specific project needs and desired functionality.

Supplies

To create this project, you'll need:

  • Arudino UNO R3
  • Jumper Wires
  • Potentiometer
  • Tilt Sensor Module
  • LED's of the Colour of Your Choice (I Picked Yellow and Red)
  • Programming Cable
  • Arudino IDE

Setup Arduino and Components

A. Connect Components.

To create our project, we will first need to set up our circuit. Firstly, we will connect both our sensors to the Arduino board, and then we will connect our output devices (LED's). We will be using jumper wires to connect all our sensors to the Arduino.

Potentiometer Connection:

  • Connect one outer pin of the Potentiometer to the 5V pin in the Arduino board.
  • Connect the other outer pin to the GND pin.
  • Connect the middle pin (wiper) to the A0 pin of the Arduino.

Tilt-Sensor Module Connection:

  • Connect the VCC pin of the tilt-sensor to the 5V pin of the Arduino.
  • Connect the GND pin in the tilt-sensor to the GND pin in the Arduino board.
  • Connect the DO pin to one of the Digital Pins in the Arduino board (in my case, I chose the 2nd pin).

Yellow LED Connection:

  • The longer leg of the LED (anode) goes in the 5th Arduino pin.
  • The shorter leg (cathode) goes in the 6th Arduino pin.

Red LED Connection:

  • The anode goes in the 9th pin.
  • The cathode goes in the 10th pin.

NOTE: Arduino generates Pulse Width Modulation (PWM) on certain digital pins. PWM is a technique used to simulate analog output using digital signals. This is commonly used in Arduino to control brightness of LED's or speed of motors. Always remember to connect LED's to digital pins with PWM function while making projects where you can control brightness. PWM-capable digital pins in the Arduino UNO are 3, 5, 6, 9, 10, 11.


B. Uploading the Code.

  • Download the Arduino IDE on your computer.
  • Copy and paste the code from the attached file in a new sketch.
  • Connect your Arduino board to your computer and upload it using the Upload button.

Downloads

Functionality Explanation

1) Setup Function:

  • Sets up pin modes for the tilt sensor (input) and LEDs (outputs).
  • Serial communication is initialized for debugging messages.


2) Loop Function:

  • Tilt Sensor Reading:
  • Reads the state of the tilt sensor connected to pin 2 (tiltState).
  • Potentiometer Reading:
  • Reads the analog value from the potentiometer connected to pin A0 (potValue).
  • Maps the analog value to a range suitable for controlling LED brightness (brightness).
  • LED Control Based on Tilt State:
  • If the tilt sensor is tilted (tiltState == HIGH):
  • Sets the brightness of the yellow LED (connected to pin 5) using analogWrite().
  • Turns off the red LED (connected to pin 6) using digitalWrite().
  • Turns off the brightness control for the red LED (pin 9) using digitalWrite().
  • Turns on the state control for the yellow LED (pin 10) using digitalWrite().
  • Prints a message to the serial monitor indicating the tilt status.
  • If the tilt sensor is not tilted (tiltState == LOW):
  • Turns off the yellow LED (pin 5) using digitalWrite().
  • Turns on the red LED (pin 6) using digitalWrite().
  • Sets the brightness of the red LED (pin 9) using analogWrite().
  • Turns off the state control for the yellow LED (pin 10) using digitalWrite().
  • Prints a message to the serial monitor indicating the tilt status.
  • Delay:
  • Introduces a delay of 100 milliseconds (delay(100)) to stabilize the loop and ensure smooth operation.


Testings and Adjustments

Serial Monitors:

  • Open the Serial Monitor in the Arduino IDE (Tools > Serial Monitor or press Ctrl+Shift+M) to view debug messages.
  • Monitor the messages to verify if the LED states correspond correctly to the tilt sensor's state and potentiometer adjustments.

Adjust Potentiometer:

  • Rotate the potentiometer knob and observe how it affects the brightness of the LEDs.
  • Ensure the LEDs respond smoothly to changes in tilt and brightness adjustments.