In this project we’ll be building a light sensitivity-based Arduino project that will trigger various sensors depending upon the light intensity in the room. You may also use sound sensor instead of light sensor.
Things used in this project,
- Arduino Uno with Cable
- Arduino Sensor Kit Base
Software Apps and Online Services
- Arduino IDE
- An active internet connection to download the package
If you would not like to download the Arduino IDE and prefer to run the project online then you may refer
Arduino Web Editor.
Before we get started let’s quickly look at all the components on Arduino Sensor Base Kit.
Image Reference: https://www.seeedstudio.com/Arduino-Sensor-Kit-Base-p-4743.html
In this project we’ll use a light sensor, buzzer, temperature & humidity sensor, LED and an OLED Screen.
Step 1 - Setting up your environment.
Since Arduino Sensor Base kit base has 10 sensors embedded just on board so instead of downloading and installing the libraries for each sensor, we can use Arduino_Sensorkit library. Go to Manage Library under Tools and search for Arduino_Sensorkit.
Please make sure you have an active internet connection for this step.
Step 2 - Writing the code for the project.
We’ll first import the Arduino Sensor kit library and declare the variables for buzzer, light sensor and the LED. Since we have the entire library loaded, we don’t need to declare the pin number for OLED and temperature & humidity sensor. If you wish you may also avoid declaring pin numbers for other components too.
- #include "Arduino_SensorKit.h"
- int buzzer = 5;
- int light_sensor = A3;
- int LED = 6;
In the following setup() function we declare the serial communication, OLED and Environment library for temperature and humidity sensor. The setFlipMode() function is used to rotate your display on the OLED screen. We then set the pin mode for buzzer and LED as output.
- void setup() {
- Serial.begin(9600);
- Oled.begin();
- Environment.begin();
- Oled.setFlipMode(true);
- pinMode(buzzer, OUTPUT);
- pinMode(LED, OUTPUT);
- }
Inside the void loop function let us understand the code briefly. We start by making the buzzer off with noTone() and switching off the LED with Low input using digitalWrite() function.
- noTone(buzzer);
- digitalWrite(LED, LOW);
For the light sensor we read the raw value from pin (A3), assign it to raw_light variable and then map the value from 0, 1023 to 0, 100 to store in the light variable.
- int raw_light = analogRead(light_sensor);
- int light = map(raw_light, 0, 1023, 0, 100);
You can set font and coordinates for the OLED display. Here we have used u8x8_font_chroma48medium8_r and the display coordinates start from (0,33).
- Oled.setFont(u8x8_font_chroma48medium8_r);
- Oled.setCursor(0, 33);
The following code inside the if statement will only run if the light sensitivity is more than 20. If this is true then we switch on the buzzer, turn on the LED and display the current temperature with light sensitivity on the OLED screen.
- if (light > 20) {
- tone(buzzer, 85);
- digitalWrite(LED, HIGH);
- Oled.print("L:");
- Oled.print(light);
- Oled.println("T:");
- Oled.print(Environment.readTemperature());
- Oled.refreshDisplay();
- } else {
- Oled.print("L:");
- Oled.print(light);
- Oled.refreshDisplay();
- }
If the light sensitivity value is less than 20, we anyway display the light value from the else statement block.
And finally, we use the delay function with value 3000 that runs the loop after 3 seconds to check the light sensitivity.
The final code looks like this,
- #include "Arduino_SensorKit.h"
- int buzzer = 5;
- int light_sensor = A3;
- int LED = 6;
- void setup() {
- Serial.begin(9600);
- Oled.begin();
- Environment.begin();
- Oled.setFlipMode(true);
- pinMode(buzzer, OUTPUT);
- pinMode(LED, OUTPUT);
- }
- void loop() {
- noTone(buzzer);
- digitalWrite(LED, LOW);
- int raw_light = analogRead(light_sensor);
- int light = map(raw_light, 0, 1023, 0, 100);
- Oled.setFont(u8x8_font_chroma48medium8_r);
- Oled.setCursor(0, 33);
- if (light > 20) {
- tone(buzzer, 85);
- digitalWrite(LED, HIGH);
- Oled.print("L:");
- Oled.print(light);
- Oled.println("T:");
- Oled.print(Environment.readTemperature());
- Oled.refreshDisplay();
- } else {
- Oled.print("L:");
- Oled.print(light);
- Oled.refreshDisplay();
- }
- delay(3000);
- }
Step 3 - Setting up your Hardware.
Place the Arduino Sensor Kit Base over Arduino and connect the Arduino to your laptop/computer. It should power up the Arduino with the kit base and you should a green LED glowing on the sensor kit.
It’s time we upload the code. You can either first compile and then upload else directly upload which should compile it too.
Output
If everything is fine then the LED and Buzzer should be ON with temperature & humudity and light sensitivity value displayed on the OLED screen, else only the light sensitivity valuse will be displayed with everything switched off.