Basic Arduino Sensor Demos (KY series)
KY-010 Optical Sensor
KY-015 Digital Temperature/Humidity Sensor
KY-033 Line Detector
KY-039 Heartbeat Monitor
// KY-010 - Optical Sensor (Like a smoke detector- measures variations in opacity
//code write by Moz for YouTube changel LogMaker360, 18-9-2015
//code belongs to this video:https://www.youtube.com/watch?v=pfy39aYNu0Y
// Example code for KY-010
// photo interrupter module
// signal wire 'S' to A0
// GND - to GND
// middle to +5v
// Piezo speaker + to pin 10
// Piezo speaker - to GND
int buzzer =10 ;// define LED Interface
int buttonpin = A0; // define the photo interrupter sensor interface
int val ;// define numeric variables val
void setup ()
{
Serial.begin(9600);
pinMode (buzzer, OUTPUT) ;// define LED as output interface
}
void loop ()
{
val = analogRead (buttonpin) ;// digital interface will be assigned a value of 3 to read val
Serial.print(val);
if(val < 50)
{
Serial.println(" Clear sky");
digitalWrite (buzzer, LOW);
}
else if (val > 50&& val < 200) // When the light sensor detects a signal is interrupted, LED flashes
{
Serial.println(" Bad moon rising");
digitalWrite (buzzer, HIGH);
sound(2);
}
else if (val > 200 && val < 750) // When the light sensor detects a signal is interrupted, LED flashes
{
Serial.println(" Trouble in the sky");
digitalWrite (buzzer, HIGH);
sound(5);
}
else // When the light sensor detects a signal is interrupted, LED flashes
{
Serial.println(" Run baby run, the house is on fire");
digitalWrite (buzzer, HIGH);
sound(10);
}
delay(1000);
}
void sound(int duration){
for (int p = 0; p < duration; p++){
for (int i = 0; i <80; i++) // Wen a frequency sound
{
digitalWrite (buzzer, HIGH) ;// send voice
delay (1) ;// Delay 1ms
digitalWrite (buzzer, LOW) ;// do not send voice
delay (1) ;// delay ms
}
for (int i = 0; i <100; i++) // Wen Qie out another frequency sound
{
digitalWrite (buzzer, HIGH) ;// send voice
delay (2) ;// delay 2ms
digitalWrite (buzzer, LOW) ;// do not send voice
delay (2) ;// delay 2ms
}
} }
//------------------------
// KY-015 Digital Temperature/Humidity Sensor
// KY-015 Humidity/Temp Sensor
int DHpin = 8;
byte dat [5];
byte read_data () {
byte data;
for (int i = 0; i < 8; i ++) {
if (digitalRead (DHpin) == LOW) {
while (digitalRead (DHpin) == LOW); // wait for 50us
delayMicroseconds (30); // determine the duration of the high level to determine the data is '0 'or '1'
if (digitalRead (DHpin) == HIGH)
data |= (1 << (7-i)); // high front and low in the post
while (digitalRead (DHpin) == HIGH); // data '1 ', wait for the next one receiver
}
}
return data;
}
void start_test () {
digitalWrite (DHpin, LOW); // bus down, send start signal
delay (30); // delay greater than 18ms, so DHT11 start signal can be detected
digitalWrite (DHpin, HIGH);
delayMicroseconds (40); // Wait for DHT11 response
pinMode (DHpin, INPUT);
while (digitalRead (DHpin) == HIGH);
delayMicroseconds (80); // DHT11 response, pulled the bus 80us
if (digitalRead (DHpin) == LOW);
delayMicroseconds (80); // DHT11 80us after the bus pulled to start sending data
for (int i = 0; i < 4; i ++) // receive temperature and humidity data, the parity bit is not considered
dat[i] = read_data ();
pinMode (DHpin, OUTPUT);
digitalWrite (DHpin, HIGH); // send data once after releasing the bus, wait for the host to open the next Start signal
}
void setup () {
Serial.begin (9600);
pinMode (DHpin, OUTPUT);
}
void loop () {
start_test ();
Serial.print ("Current humdity =");
Serial.print (dat [0], DEC); // display the humidity-bit integer;
Serial.print ('.');
Serial.print (dat [1], DEC); // display the humidity decimal places;
Serial.println ('%');
Serial.print ("Current temperature =");
Serial.print (dat [2], DEC); // display the temperature of integer bits;
Serial.print ('.');
Serial.print (dat [3], DEC); // display the temperature of decimal places;
Serial.println ('C');
delay (700);
}
//------------------
//-----------------
// KY-033 Line Detector
// looks for a black or white line right under the detector. Can build a line-following robot.
// VERY SHORT RANGE (centimetres)
int Led = 13 ;// define LED Interface
int buttonpin = 3; // define the obstacle avoidance sensor interface
int val ;// define numeric variables val
void setup ()
{
pinMode (Led, OUTPUT) ;// define LED as output interface
pinMode (buttonpin, INPUT) ;// define the obstacle avoidance sensor output interface
Serial.begin(9600);
}
void loop ()
{
val = digitalRead (buttonpin) ;// digital interface will be assigned a value of 3 to read val
Serial.println(val);
if (val == HIGH) // When the obstacle avoidance sensor detects a signal, LED flashes
{
digitalWrite (Led, HIGH);
}
else
{
digitalWrite (Led, LOW);
}
}
//------------
//KY-039 Heartbeat Monitor
// measures transmission of IR radiation from LED through finger to the LDR underneath
// Pulse Monitor Test Script
// best results when circuit is out of direct light.
// under dark piece of cloth is good.
int sensorPin = A0;
double alpha = 0.75;
int period = 100;
double change = 0.0;
double minval = 0.0;
void setup ()
{
Serial.begin (9600);
}
void loop ()
{
static double oldValue = 0;
static double oldChange = 0;
int rawValue = analogRead (sensorPin);
double value = alpha * oldValue + (1 - alpha) * rawValue;
Serial.print (rawValue);
Serial.print (",");
Serial.println (value*1.12/10); // add 12% factor
oldValue = value;
delay (period);
}