Light Sensor using Arduino

Introduction

In this project, a simple light sensor is designed using LDR. The project is built around Arduino.

Components required

  • Arduino UNO
  • Light Dependent Resistor (LDR)
  • 100 KΩ POT
  • Buzzer

Circuit Diagram

Working

A 100 KΩ POT and the LDR form a voltage divider and the output of the voltage divider is given to the analog input A0 of Arduino. A buzzer is connected to pin 11 of Arduino.

When the LDR detects a light over certain intensity, the Arduino will trigger the buzzer. When the intensity of light decreases, the buzzer is turned off.

The 100 KΩ POT used in the voltage divider network can be used to adjust the intensity levels at which the buzzer is triggered.

Code

int sensorPin = A0; // select the input pin for the potentiometer
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
Serial.begin(9600);
pinMode(11,OUTPUT);
}
void loop()
{
sensorValue=analogRead(sensorPin);
if(sensorValue <= 14)
digitalWrite(11,HIGH);
else
digitalWrite(11,LOW);
Serial.println(sensorValue);
delay(2);
}

This circuit is similar to dark sensor circuit using BC547 transistor.

How to connect 16*2 LCD display Arduino UNO

To Connect 16*2 LCD Display Arduino Uno we will use the previous project to capture temperature and display on console.

Parts required for the project:

  1. Arduino IDE to program the code and upload
  2. OneWire and DallasTemperatre library for the Arduino and DS18B20
  3. One DS18B20 digital temperature sensor
  4. Arduino UNO R3
  5. 16*2 LCD display
  6. Jumper wires
  7. Breadboard/PC/General purpose board
  8. Arduino UNO cable
  9. wires

Steps 1: Wiring Arduino and DS18B20

  • The wiring, of a 1-wire interface, is super simple.
  • The GND pin of the DS18B20 goes to GND on the Arduino. [black]
  • The Vdd pin of the DS18B20 goes to +5V on the Arduino. [red]
  • The Data pin of the DS18B20 goes to a (digital) pin of your choice on the Arduino, in this example I used Pin 7
  • Add a pull-up resistor of 4.7 KΩ. as shown in the schematic diagram. One end of resistor connecting Vdd and another end connecting data pin.

Step 2: Connecting the LCD display

  • VSS –> GND Arduino
  • VDP –> 5V Arduino
  • VO –> output potentiometer (potentiometer VCC -> 5V Arduino, potentiometer GND -> Arduino GND).
  • RS –> pin 12 Arduino
  • RW –> GND Arduino
  • E –> pin 11 Arduino
  • D4 –> pin 5 Arduino
  • D5 –> pin 4 Arduino
  • D6 –> pin 3 Arduino
  • D7 –> pin 2 Arduino
  • A –> 5V Arduino with 1.2 k resistor
  • K –> GND Arduino

 LCD display Arduino UNO breadboard diagram

Step 3: WRITING CODE AND UPLOADING

Machine generated alternative text: fritzing

#include<OneWire.h>
#include<DallasTemperature.h>
#include<LiquidCrystal.h>

// Data wire is plugged into digital pin2
#define ONE_WIRE_BUS 7
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
//LCD display pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
double temperature;
void setup(void)
{
 Serial.begin(9600);
 //Serial.println("Temperature Demo");
 sensors.begin();
 lcd.begin(16, 2);
 lcd.print("hello, WORLD");

}
void loop()
{
 sensors.requestTemperatures(); // send command to get temperatures
 delay(500);
 temperature= sensors.getTempCByIndex(0);
 delay(1000);
 lcd.display();
 lcd.setCursor(0, 1);
 lcd.print("Temp: ");
 lcd.setCursor(7, 1);
 lcd.print(temperature);
 lcd.print(" C");
 
}

LCD display Arduino UNO Schematic

Screenshot of the Project output

LCD display Arduino UNO project screenshot

How to Measure temperature with Arduino and DS18B20 sensor?

In this example project we will be combining an Arduino and DS18B20 sensor. The DS18B20 is also called 1-wire digital temperature sensor

Arduino and DS18B20 Temperature Sensor The DS18B20 comes in different forms and shapes, so you have plenty of choice when deciding which one works best for you. There are 3 variations available: 8-Pin SO (150 mils), 8-Pin µSOP, and 3-Pin TO-92.

I have used waterproof version as shown below.

ds18b20-waterproof

Note: DS18B20 is quite versatile. It can be powered through the data line (so called “parasite” mode, which requires only 2 wires versus 3 in normal mode), it operates in a 3.0V to 5.5V range, measures Temperatures from -55°C to +125°C (-67°F to +257°F) with and ±0.5°C Accuracy (from -10°C to +85°C). It converts a temperature in 750ms or less to a up to 12 bits value. Another cool feature is that you can connect up to 127 of these sensors in parallel, and read each individual temperature.

Things you need to get Arduino and DS18B20 sensor work:

  1. Arduino IDE to program the code and upload
  2. OneWire and DallasTemperatre library for the Arduino and DS18B20
  3. One DS18B20 digital temperature sensor
  4. Arduino UNO R3
  5. Jumper wires
  6. Breadboard/PC/General purpose board
  7. Arduino UNO cable

Below is the schematic diagram for the same.

Schemaic_arduino_ds18b20_temperature_sensor

 

 

Step 2: Installing and loading OneWire and DallasTemperature Library

Unzip the downloaded zip file. Make sure that folder name is OneWire, which contains the library. Drag it into the Library folder of Arduino IDE. Alternatively you can use Sketch-> Import Library -> Add Library option of Arduino IDE and select the Zip file.

Step3: Writing code and uploading

#include<OneWire.h>
#include<DallasTemperature.h>

// Data wire is plugged into digital pin2
#define ONE_WIRE_BUS 2

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);
void setup(void)
{
 Serial.begin(9600);
 Serial.println("Temperature Demo");
 sensors.begin();
}

void loop()
{
 Serial.print(" Fetching temperature...");
 sensors.requestTemperatures(); // send command to get temperatures
 Serial.println("Done..");
 Serial.println("Temperature is ");
 Serial.println(sensors.getTempCByIndex(0));
 delay(1000);
}


Output will be shown as follows:

Arduino DS18b20 - Output

Screenshot of the above example:

Arduino and a DS18B20 sensor 1

We can modify this to display it in LCD display. For details on how to display the temperature on LCD display visit my post How to connect 16*2 LCD display Arduino UNO

Servo Motor Control using Arduino

Following post will explain Servo motor control using Arduino UNO 3.

Servo motors have three wires: power, ground, and signal. The power wire is typically red, and should be connected to the 5V pin on the Arduino board. The ground wire is typically black or brown and should be connected to a ground pin on the board. The signal pin is typically yellow or orange and should be connected to pin 9 on the board.

The potentiometer should be wired so that its two outer pins are connected to power (+5V) and ground, and its middle pin is connected to analog input 0 on the board.

Figure 1 – Schematic Diagram

servomotor-arduino_bb

Figure 2- Circuit Diagram

servomotor-arduino_schem

 

 

Complie and upload the following code.

#include <Servo.h>
Servo myservo;  // create servo object to control a servo
int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin

void setup() {
  myservo.attach(9);  
// attaches the servo on pin 9 to the servo object
}

void loop() {
 val = analogRead(potpin);   
 // reads the value of the   potentiometer (value between 0 and 1023)
 val = map(val, 0, 1023, 0, 180); 
 // scale it to use it with the  
 servo (value between 0 and 180)
 myservo.write(val); 
// sets the servo position   according to the scaled value
 delay(15);   
// waits for the servo to get there
}

Next we will see how to measure temperature using DS18B20.