Microphone Sensor: Sample Code For Sound Sensitive Light
Microphone Sensor: Sample Code For Sound Sensitive Light
This sensor is used for sound detection. The digital output sends a
high signal when the sound the sound intensity reaches a certain
threshold. The threshold-sensitivity can be adjusted via
potentiometer on the sensor.
SAMPLE CODE FOR SOUND SENSITIVE LIGHT:
int ledPin=13;
int sensorPin=7;
boolean val =0;
void setup(){
pinMode(ledPin, OUTPUT);
pinMode(sensorPin, INPUT);
Serial.begin (9600);
}
You can use this module to measure the absolute pressure of the
environment. By converting the pressure measures into altitude,
you have a reliable sensor for determining the height of your robot
or projectile, for example. This sensor also measures temperature
and humidity.
SAMPLE CODE:
#include <SFE_BMP180.h>
#include <Wire.h>
SFE_BMP180 pressure;
void setup()
{
Serial.begin(9600);
Serial.println("REBOOT");
void loop()
{
char status;
double T,P,p0,a;
Serial.println();
Serial.print("provided altitude: ");
Serial.print(ALTITUDE,0);
Serial.print(" meters, ");
Serial.print(ALTITUDE*3.28084,0);
Serial.println(" feet");
status = pressure.startTemperature();
if (status == 1)
{
// Wait for the measurement to complete:
delay(status);
// Retrieve the completed temperature
measurement:
// Note that the measurement is stored in the
variable T.
// Function returns 1 if successful, 0 if
failure.
status = pressure.getTemperature(T);
if (status == 1)
{
// Print out the measurement:
Serial.print("temperature: ");
Serial.print(T,2);
Serial.print(" deg C, ");
Serial.print((9.0/5.0)*T+32.0,2);
Serial.println(" deg F");
status = pressure.startPressure(3);
if (status != 0)
{
// Wait for the measurement to complete:
delay(status);
status = pressure.getPressure(P,T);
if (status == 1)
{
// Print out the measurement:
Serial.print("absolute pressure: ");
Serial.print(P,2);
Serial.print(" mb, ");
Serial.print(P*0.0295333727,2);
Serial.println(" inHg");
p0 = pressure.sealevel(P,ALTITUDE); //
we're at 1655 meters (Boulder, CO)
Serial.print("relative (sea-level)
pressure: ");
Serial.print(p0,2);
Serial.print(" mb, ");
Serial.print(p0*0.0295333727,2);
Serial.println(" inHg");
a = pressure.altitude(P,p0);
Serial.print("computed altitude: ");
Serial.print(a,0);
Serial.print(" meters, ");
Serial.print(a*3.28084,0);
Serial.println(" feet");
}
else Serial.println("error retrieving
pressure measurement\n");
}
else Serial.println("error starting pressure
measurement\n");
}
else Serial.println("error retrieving temperature
measurement\n");
}
else Serial.println("error starting temperature
measurement\n");
delay(5000);
}
Rotary Encoder Module Brick Sensor
Development Board
void setup() {
pinMode (pinA,INPUT);
pinMode (pinB,INPUT);
pinALast = digitalRead(pinA);
Serial.begin (9600);
}
void loop() {
aVal = digitalRead(pinA);
if (aVal != pinALast){ // Means the knob is
rotating
// if the knob is rotating, we need to determine
direction
// We do that by reading pin B.
if (digitalRead(pinB) != aVal) { // Means pin A
Changed first - We're Rotating Clockwise
encoderPosCount ++;
bCW = true;
} else {// Otherwise B changed first and we're
moving CCW
bCW = false;
encoderPosCount--;
}
Serial.print ("Rotated: ");
if (bCW){
Serial.println ("clockwise");
}else{
Serial.println("counterclockwise");
}
Serial.print("Encoder Position: ");
Serial.println(encoderPosCount);
}
pinALast = aVal;
}
Humidity and Rain Detection Sensor Module
This is a rain sensor. It’s used for all kinds of weather monitoring.
SAMPLE CODE:
const int sensorMin = 0;
const int sensorMax = 1024;
void setup()
{
Serial.begin(9600);
}
void loop()
{
int sensorReading = analogRead(A0);
int range = map(sensorReading, sensorMin,
sensorMax, 0, 3);
switch (range)
{
case 0:
Serial.println("RAINING");
break;
case 1:
Serial.println("RAIN WARNING");
break;
case 2:
Serial.println("NOT RAINING");
break;
}
delay(1000);
}
SW-420 Motion Sensor Module Vibration
Switch Alarm
void loop() {
int val;
val = digitalRead(vibr_pin);
if (val == 1)
{
digitalWrite(LED_Pin, HIGH);
delay(1000);
digitalWrite(LED_Pin, LOW);
delay(1000);
}
else
digitalWrite(LED_Pin, LOW);
}
Speed Sensor Module
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
pinMode(sensor,INPUT_PULLUP);
lcd.setCursor(0,0);
lcd.print(" STEPS - 0");
lcd.setCursor(0,1);
lcd.print(" RPS - 0.00");
}
void loop()
{
start_time=millis();
end_time=start_time+1000;
while(millis()<end_time)
{
if(digitalRead(sensor))
{
steps=steps+1;
while(digitalRead(sensor));
}
lcd.setCursor(9,0);
lcd.print(steps);
lcd.print(" ");
}
temp=steps-steps_old;
steps_old=steps;
rps=(temp/20);
lcd.setCursor(9,1);
lcd.print(rps);
lcd.print(" ");
}
Accelerometer Module
void setup() {
Serial.begin(9600);
pinMode(groundpin, OUTPUT);
pinMode(powerpin, OUTPUT);
digitalWrite(groundpin, LOW);
digitalWrite(powerpin, HIGH);
}
void loop() {
Serial.print(analogRead(xpin));
Serial.print("\t");
Serial.print(analogRead(ypin));
Serial.print("\t");
Serial.print(analogRead(zpin));
Serial.println();
delay(100);
}
DHT11 Temperature and Humidity Sensor
void setup()
{
Serial.begin(9600);
delay(500);
Serial.println("DHT11 Humidity & temperature
Sensor\n\n");
delay(1000);
}
void loop()
{
DHT.read11(dht_apin);
Serial.print("Current humidity = ");
Serial.print(DHT.humidity);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(DHT.temperature);
Serial.println("C ");
delay(5000);
}
Knock Sensor
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
val = analogRead(knockSensor);
if (val >= THRESHOLD) {
statePin = !statePin;
digitalWrite(ledPin, statePin);
Serial.println("Knock!");
}
delay(100);
}
Metal Touch Sensor
This is the KY-036 sensor module for detecting human touch. Can
be used to control projects based on human or animal touches to
metal connected object. Great for making a touch lamp project.
SAMPLE CODE:
int vibr_pin = 3;
int LED_Pin = 13;
void setup() {
pinMode(vibr_pin, INPUT);
pinMode(LED_Pin, OUTPUT);
}
void loop() {
int val;
val = digitalRead(vibr_pin);
if (val == 1)
{
digitalWrite(LED_Pin, HIGH);
delay(1000);
digitalWrite(LED_Pin, LOW);
delay(1000);
}
else
digitalWrite(LED_Pin, LOW);
}