Skip to main content

Arduino Tutorial: Detecting Light with a Light Sensing Resistor for beginners

Learn How to Use a Light Sensing Resistor with Arduino

A girl with a globe and mechanical things around her ,its moody


In a world where technology is king, one lone inventor decided to create something truly chaotic. 



A girl soldering with an arduino,blonde woman

Using nothing but an Arduino, a light sensor (LDR), and a buzzer, they created a machine that could turn light into sound.


So, you want to know how I made this chaotic little machine? Let me show you the code.


Diagram showing the circuitry of a light sensing circuit


Resistor to Detect Light with Arduino

components for the circuit:

1. Arduino uno
2.buzzer
3.battery
4.LDR (light dependent resistor).

Explaining the code:

The code is for an Arduino microcontroller, which is a small computer that can be programmed to interact with the physical world using sensors and actuators. In this case, the Arduino is connected to a light sensor and a buzzer.


int lightpin= A0;
int buzzpin = 8;
int lightval;
int delaytime;


The first four lines of the code set up variables to store information that will be used later in the code. The `lightpin` variable is set to `A0`, which is the analog input pin connected to the light sensor. The `buzzpin` variable is set to `8`, which is the digital output pin connected to the buzzer. The `lightval` variable will be used to store the value read from the light sensor, and the `delaytime` variable will be used to store the amount of time the buzzer should be on and off.


void setup(){
pinMode(lightpin,INPUT);
pinMode(buzzpin,OUTPUT);
Serial.begin(9600);

}

The `setup` function runs once when the Arduino is turned on or reset. In this function, the `pinMode` function is used to set the `lightpin` as an input and the `buzzpin` as an output. This tells the Arduino that it should read data from the light sensor and send data to control the buzzer. The `Serial.begin(9600)` function initializes serial communication at a baud rate of 9600 so you can see what's happening in the code on your computer.

void loop(){
  lightval = analogRead(lightpin);
  delaytime =(9.0/550.)*lightval-(9.*200./550.)+1.0;
  Serial.println(lightval);
  digitalWrite(buzzpin,HIGH);
  delay(delaytime);
  digitalWrite(buzzpin,LOW);
  delay(delaytime);
}


The `loop` function runs over and over again as long as the Arduino is on. In this function, the `analogRead` function is used to read the value of the light sensor and store it in the `lightval` variable. The value of `lightval` will be between 250 and 750 with higher values indicating more light.

delaytime =(9.0/550.)*lightval-(9.*200./550.)+1.0;


The next line calculates how long the buzzer should be on and off based on the value of lightval. The formula is obtained using slope. We prefer the sound the buzzer makes at 1ms to 10 ms. The value of the light sensor when light is shown on it versus when light is not shown on it is 250 to 750. This can be illustrated using a graph as shown below:


A picture of a graph with 1 to 10 ms on the vertical and 250 to 750 at the horizontal and  an equation that was gotten using equation of a straight line when there is no intercept. That is the equation used  in the code



So, as you can see from my ugly graph, I used the equation of a straight line when the intercept is not zero, which is (y-y1) = m(x-x1). I got ‘m’ by using the measured value of light and the value I wanted for the buzzer.it 
essentially determines how fast or slow the buzzer should beep based on how much light is detected by the sensor.


Serial.println(lightval);

The `Serial.println(lightval)` function prints the value of `lightval` to the serial monitor so you can see it on your computer. This can be useful for debugging or understanding what's happening in the code.


digitalWrite(buzzpin,HIGH);
  delay(delaytime);
  digitalWrite(buzzpin,LOW);
  delay(delaytime);

The next three lines use the `digitalWrite` and `delay` functions to turn the buzzer on and off at a frequency determined by the value of `delaytime`. The `digitalWrite(buzzpin,HIGH)` line turns on the buzzer by sending a signal to `buzzpin`. The `delay(delaytime)` line waits for an amount of time determined by `delaytime`. The `digitalWrite(buzzpin,LOW)` line turns off the buzzer by stopping the signal to `buzzpin`. The final `delay(delaytime)` line waits for another amount of time determined by `delaytime`, then ends the loop and starts it over again from the beginning.
int lightpin= A0;
int buzzpin = 8;
int lightval;
int delaytime;
void setup(){
pinMode(lightpin,INPUT);
pinMode(buzzpin,OUTPUT);
Serial.begin(9600);

}
void loop(){
  lightval = analogRead(lightpin);
  delaytime =(9.0/550.)*lightval-(9.*200./550.)+1.0;
  Serial.println(lightval);
  digitalWrite(buzzpin,HIGH);
  delay(delaytime);
  digitalWrite(buzzpin,LOW);
  delay(delaytime);
}

Comments

Popular posts from this blog

Thevenin’s Theorem: A Beginner’s Guide

                                        table of content  Introduction History of Thevenin’s theorem Basic circuit analysis concepts Voltage, current, and resistance Ohm’s law Thevenin’s theorem: principles and applications Statement of the theorem Finding the Thevenin equivalent circuit What is Thevenin’s theorem? When should you use Thevenin’s theorem? How do you apply Thevenin’s theorem to a circuit Conclusion . Introduction Electrical engineers can effectively convert complicated circuits into smaller equivalent circuits by using Thevenin's theorem. The French telegraph engineer Léon Charles Thévenin is honored by having his theorem called in his honor. He proposed it in 1883. History of Thevenin’s theorem Hermann von Helmholtz, a German scientist, independently derived Thevenin's theorem in 1853; Léon Charles Thévenin did ...

WHAT IS WEBSCRAPING IN PYTHON

  Web scraping is a technique that allows you to extract data from websites and store it in a format of your choice. It can be useful for various purposes, such as market research, price comparison, content analysis, and more. In this blog post, i will show you everything a beginner needs to know about web scraping, from the basics to some advanced tips and tricks.   What is web scraping?   Web scraping is the process of programmatically retrieving information from web pages. It involves sending requests to web servers, parsing the html code of the web pages, and extracting the data you want. Web scraping can be done manually, by copying and pasting data from a website, or automatically, by using a software tool or a programming language.   Why web scrape?   Web scraping can help you access data that is not available through an api or a downloadable file. For example, you may want to scrape product reviews from an e-commerce website, or news ...

Study Linear Regression: A Beginner’s Guide with c++

Table of content: • Introduction • What is Linear Regression? • Simple vs. Multiple Linear Regression • Implementing Linear Regression in C++ • Complex Linear Regression • Conclusion • References Understanding Linear Regression: A Beginner’s Guide Linear regression is a powerful statistical tool that allows us to understand the relationship between two or more variables. It is widely used in many fields, including finance, economics, and engineering, to make predictions and analyze data. In this article, we will explore the basics of linear regression, including what it is, how it works, and how to implement it in C++. We will also discuss the differences between simple and multiple linear regression and provide examples to help you understand these concepts. What is Linear Regression? At its core, linear regression is a method for finding the line of best fit that describes the relationship between two continuous variables. This line can be used to make predictions about o...