DEV Community

Cover image for A Beginner's Journey: Discovering the Magic of Light Detection with Arduino and a Light Sensing Resistor
maryclareok
maryclareok

Posted on

A Beginner's Journey: Discovering the Magic of Light Detection with Arduino and a Light Sensing Resistor

an image of a girl with a ball and steam engines around her
In a world where technology is king, one lone inventor decided to create something truly chaotic. 

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

a circuit diagram of the project,a circuit that controls the tone of the buzzer as the light turns on or off

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

'int lightpin= A0;'

This line defines the pin number for the light sensor. We're using pin A0 on the Arduino.

'int buzzpin = 8;'

This line defines the pin number for the buzzer. We're using pin 8 on the Arduino.

'''int lightval;
int delaytime;'''

These two lines define variables to hold the value of the light sensor and the delay time for the buzzer.

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

In the setup function, we set the pinMode for the light sensor and buzzer pins. The light sensor is set as an INPUT because we're reading data from it, while the buzzer is set as an OUTPUT because we're sending data to it. We also start serial communication at a baud rate of 9600 so we can see the value of the light sensor in the serial monitor.
'''
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);
}'''

In the loop function, we read the value of the light sensor using analogRead and store it in the lightval variable. We then calculate the delay time for the buzzer based on that value using a mathematical formula. The formula takes into account the range of possible values for the light sensor and maps it to a range of delay times for the buzzer.
click the link for more Indepth explanation.
We then print the value of the light sensor to the serial monitor using Serial.println so we can see how it changes in real-time.
Next, we use digitalWrite to turn the buzzer on by sending a HIGH signal to its pin. We then use delay to wait for a period of time equal to our calculated delay time before turning the buzzer off again by sending a LOW signal to its pin. We then use another delay to wait for the same period of time before starting the loop again.
This sequence of turning the buzzer on and off at a frequency determined by our calculated delay time creates a sound that changes based on the amount of light detected by the sensor.
'''
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);
}'''

Top comments (1)

Collapse
 
mericlre profile image
maryclareok

hope it was helpful and entertaining