Ways to Realize Arduino "sound and Light Dual Control Lamp"

by helen-tech in Circuits > Arduino

111 Views, 3 Favorites, 0 Comments

Ways to Realize Arduino "sound and Light Dual Control Lamp"

sound and light dual control lamp.jpg

In Arduino, we can use sound sensors and light sensors to detect the sound and light intensity of the surrounding environment respectively, and then realize the "sound and light dual control lamp" function through Arduino IDE code programming, Python code programming and Mind+ graphical programming.

Supplies

1 Arduino UNO motherboard


1 sound sensor


1 light sensor


1 red LED light


1 breadboard


Several Dupont wires of various colors

Experimental Equipment and Circuit Connection

First, insert the long leg of the LED into pin 13 and the short leg into the GND ground pin; then connect the 5V and GND of the Arduino to the edge slots of the breadboard (marked in red and blue), and then use the DuPont line to connect the VCC positive pole and GND ground of the sound sensor and light sensor to the breadboard; then connect the OUT signal output of the sound sensor to the digital pin 2 of the Arduino, and the AO analog signal output of the light sensor to the A2 analog pin of the Arduino; finally, connect the Arduino to the USB port of the computer via a data cable.

Arduino IDE Code Programming to Achieve "sound and Light Dual Control Lamp"

Arduino IDE Code Programming.jpg

First, in the Arduino IDE, use the statements "int led = 13;" and "int SoundSensorPin = 2;" to declare the connection pins of the LED light and the sound sensor respectively; then, in the setup() function, use the statements "pinMode(led,OUTPUT);" and "pinMode(SoundSensorPin,INPUT);" to set pin 13 to output mode and pin 2 to input mode.


In the loop() function, first use the statement "int LightSensorValue = analogRead(A2);" to create the variable LightSensorValue, whose value is the data transmitted by the light sensor through the A2 analog pin; then use the statement "int SoundSensorValue = digitalRead(SoundSensorPin);" to create the variable SoundSensorValue, whose value is the data transmitted by the sound sensor through the 2 digital pin.


After experimental testing, it was found that the light data measured in normal indoor light is about 220, the light data measured when covering the light sensor with a hand (simulating low light conditions) is about 930, and the light data measured when illuminating the light sensor with a flashlight is about 20, which means that the stronger the light, the smaller the data value, and the weaker the light, the larger the data value. By adjusting the potentiometer on the sound sensor to make it within a more suitable detection environment and surrounding sound intensity range, it is found that the detection value when "no sound" is 1 (corresponding to HIGH high level), and the detection value when "there is sound" is 0 (corresponding to LOW low level). Therefore, in the "if...else..." selection structure, the judgment condition is constructed as "SoundSensorValue == LOW and LightSensorValue >= 500", that is, to judge whether the detection value of the sound sensor is 0 and whether the detection value of the light sensor is above 500. When these two conditions are met at the same time, the LED light should be controlled to emit light: "digitalWrite(led,HIGH);", and it should be kept lit for 3 seconds: "delay(3000);". If the condition is not met, the LED is off, which is achieved in the else branch through the statement "digitalWrite(led,LOW);"

Compile and Upload the Program to Arduino

Compile And Upload The Program To Arduino.jpg

Test the sound and light dual-control lamp: When the indoor light is in normal lighting state, no matter how loud the music is in the speaker, the LED light will not light up; if you cover the light sensor with your hand and play music at this time, the LED light will light up (as shown in the picture). Similarly, when the light sensor is covered and the surrounding environment is kept in a relatively quiet state, the LED light will not light up

Python Code Programming Realizes "sound and Light Dual Control Lamp"

Python Code Programming.png

With the help of the pinpong library module, Arduino is programmed in the Python IDLE programming environment. First, import the time and pinpong library modules through the "import time" and "from pinpong.board import Board,Pin" statements; the "Board("uno").begin()" statement is used to implement the initialization operation of the Arduino uno board type (including automatic identification of the port number connected to the motherboard); then, three variables, led, SoundSensor, and LightSensor, are established, corresponding to the declaration settings of the LED light, sound sensor, and light sensor respectively. The statement "led = Pin(Pin.D13, Pin.OUT)" is used to set the LED light connected to pin 13 as the signal output end, the statement "SoundSensor = Pin(Pin.D2, Pin.IN)" is used to set the sound sensor connected to digital pin 2 as the signal input end, and the statement "LightSensor = Pin(Pin.A2, Pin.ANALOG)" is used to set the light sensor connected to analog pin A2 as the analog signal end.


In the "while True:" loop structure, first create two variables, Sound_Sensor_Value and Light_Sensor_Value, to store the data read by the sound sensor and light sensor respectively. Note that one is a digital signal and the other is an analog signal: "Sound_Sensor_Value = SoundSensor.read_digital()", "LightSensor.read_analog()"; then, create an "if...else..." selection structure, the judgment condition is "Sound_Sensor_Value == 0 and Light_Sensor_Value >= 500", that is, to judge whether the data value monitored by the sound sensor is 0 (corresponding to the low-level "sound" state) and the data value monitored by the light sensor is greater than or equal to 500. If so, execute the operation of making the LED light up for 3 seconds: "led.write_digital(1)", "time.sleep(3)"; otherwise, write a low level to the LED connection pin through the statement "led.write_digital(0)", that is, "turn off the light" (as shown in Figure 4).


Save the program and press the function key F5 to run the program to test the "sound and light dual-control lamp". The effect is consistent with programming using the Arduino IDE code.

Mind+ Graphical Programming Realizes "sound and Light Dual Control Lamp"

Mind+ Graphical Programming Realizes.jpg

Run Mind+ to enter the graphical programming interface, and combine the "building block" statements according to the code programming structure just now. The most critical thing is to build the judgment condition in the "if...otherwise..." selection branch. It is also to judge whether the two conditions that the sound sensor monitoring data connected to the No. 2 digital pin is 0 and the light sensor monitoring data connected to the A2 analog pin is greater than or equal to 500 are met at the same time. If it is true, the LED light is controlled to light up for 5 seconds, otherwise it is controlled to keep the LED light off (as shown in the figure).


Click the "Upload to Device" item, and after the "Upload Successful" prompt appears, test the Arduino's "sound and light dual control lamp" again, and the effect is still the same as using code programming.