VEEROBOT Docs
StoreBlogWiki
  • 🌐Introduction
  • ROS Based Robots
    • 🐺Wolf Robot
      • ROS Introduction
      • Getting Started with ROS 2
      • Introduction to Wolf Robot
      • ROS 2 Setup
      • ROS Concepts
      • Using Wolf Examples
      • Creating a URDF File
      • Visualising in Gazebo
      • Controlling motors from ROS
      • LiDAR Basics & Control
      • Using a Camera with ROS2
      • ROS 2 Cleanup
      • ROS 2 Controller
      • Teleoperation
      • Slam Navigation
      • Wolf : Conclusion
  • Arduino Based Robots
    • 🚜Micro:Xbot
      • Preparations before First Use
      • Programming the Board
      • Powering the Board
      • Pin Mapping
      • Basic Programs
        • Blinking LED
        • Buzzer Program
        • Fading LED
        • RGB LED
        • Light Detection
        • Battery Voltage
        • Line Follower Module
        • Ultrasonic Sensor
        • Infrared Remote Control
        • Motor Control
        • Bluetooth Control
        • Display Test
        • Test Me
      • Robot Assembly
      • Advanced Programs
        • Bluetooth Controlled Robot
        • Obstacle Avoidance Robot
        • Line Follower Robot
  • ROS2 Repo
    • Introduction
    • Page 1
      • Sub Page 1
    • Page 2
Powered by GitBook
On this page
  1. Arduino Based Robots
  2. Micro:Xbot
  3. Basic Programs

Light Detection

Control LED brightness depending on the amount of light

In this program we will learn two different concepts.

  1. Control brightness of LED depending on the amount of light that falls on one of the Light Detecting Resistor (LDR)

  2. Display the analog value read by the two LDR's on a serial monitor.

To begin with, upload the program below and monitor the LED. Move the hand closer to dim the LED. Flash a light on the LDR and the LED turns brighter. The inverse effect (LED brightens with lesser light) is also possible by changing the threshold_val and modifying the if condition.

/*
  Light Detecting: This program controls LED brightness depending on the amount of Light it detects

  At VEEROBOT, we invest time and resources providing this open source code,
  Please support VEEROBOT and open-source hardware by purchasing products
  from us @ http://veerobot.com
  -----------------------------------------------------------------------------
  You are free to redistribute it and/or modify it under the terms of the GNU
  Lesser General Public License as  published by the Free Software Foundation,
  either version 3 of the License, or (at your option) any later version.

  This Code is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Lesser General Public License for more details.

  See <http://www.gnu.org/licenses/>
*/

#include "config.h"
int ldr1Reading = 0;      // to store analog value of LDR1
int ldr2Reading = 0;      // to store analog value of LDR2
int LEDBrightness = 0;    // to store the value of LED Brightness
int threshold_val = 50;   // Add a threshold to control on and off of LED

void setup() {
  Serial.begin(9600);     // initializing serial communication.
  pinMode(LED_BUILTIN, OUTPUT);   // Defining LED pin as output
}

void loop() {
  ldr1Reading = analogRead(LDR1);    // Reading LDR1 value
  ldr2Reading = analogRead(LDR2);    // Reading LDR2 value
  Serial.println((String)"LDR1:" + ldr1Reading + " LDR2:" + ldr2Reading); // Printing to serial monitor

  if (ldr1Reading > threshold_val) {                 // Condition to make LED ON
    LEDBrightness = map(ldr1Reading, 0, 1023, 0, 255); // Converting LDR to LED Brightness
    analogWrite(LED_BUILTIN, LEDBrightness); // Writing Brightness to LED
  }
  else {
    analogWrite(LED_BUILTIN, 0);             // If LDR is below threshold make LED OFF
  }
  delay(300);                               // delay to make output readable on serial monitor
}

Serial Monitor is an essential tool when creating projects with Arduino. It can be used as a debugging tool, testing out concepts or to communicate directly with the Arduino board. Its job is to allow you to send messages from your computer to an Arduino board (over USB) and also to receive messages from the Arduino board.

Arduino IDE has Serial Monitor tool integrated with the editor, which means that no external software is required.The command 'Serial.begin(9600)' starts serial communication, so that Arduino can send out commands through the USB connection. The value 9600 is called the 'baud rate' of the connection. This is how fast the data is to be sent. You can change this to a higher value, but you will also have to change the Arduino Serial monitor to the same value.

Once the code is uploaded, Tools -> Serial Monitor opens a new window where all the messages are displayed. You should be able to see LDR values printed on the monitor. For Arduino versions > 2.0, Serial monitor is built into the IDE and does not open a new window.

PreviousRGB LEDNextBattery Voltage

Last updated 2 years ago

🚜