0% found this document useful (0 votes)
53 views

Microphone Sensor: Sample Code For Sound Sensitive Light

This sensor detects knocks or vibrations. When a vibration above a threshold is detected, it outputs a high signal. The threshold sensitivity can be adjusted. Sample code shows how to use it to trigger an LED when knocks are detected.

Uploaded by

Lolness XD
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Microphone Sensor: Sample Code For Sound Sensitive Light

This sensor detects knocks or vibrations. When a vibration above a threshold is detected, it outputs a high signal. The threshold sensitivity can be adjusted. Sample code shows how to use it to trigger an LED when knocks are detected.

Uploaded by

Lolness XD
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Microphone Sensor

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);
}

void loop (){


val =digitalRead(sensorPin);
Serial.println (val);
// when the sensor detects a signal above the
threshold value, LED flashes
if (val==HIGH) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
}
Digital Barometric Pressure Sensor Board

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>

// You will need to create an SFE_BMP180 object, here


called "pressure":

SFE_BMP180 pressure;

#define ALTITUDE 1655.0 // Altitude of SparkFun's HQ


in Boulder, CO. in meters

void setup()
{
Serial.begin(9600);
Serial.println("REBOOT");

// Initialize the sensor (it is important to get


calibration values stored on the device).
if (pressure.begin())
Serial.println("BMP180 init success");
else
{
Serial.println("BMP180 init fail\n\n");
while(1);
}
}

void loop()
{
char status;
double T,P,p0,a;

// Loop here getting pressure readings every 10


seconds.
// We're using a constant called ALTITUDE in this
sketch:

Serial.println();
Serial.print("provided altitude: ");
Serial.print(ALTITUDE,0);
Serial.print(" meters, ");
Serial.print(ALTITUDE*3.28084,0);
Serial.println(" feet");

// If you want to measure altitude, and not


pressure, you will instead need
// to provide a known baseline pressure. This is
shown at the end of the sketch.

// You must first get a temperature measurement to


perform a pressure reading.

// Start a temperature measurement:


// If request is successful, the number of ms to
wait is returned.

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");

// Start a pressure measurement:


// The parameter is the oversampling setting,
from 0 to 3 (highest res, longest wait).
// If request is successful, the number of ms
to wait is returned.
// If request is unsuccessful, 0 is returned.

status = pressure.startPressure(3);
if (status != 0)
{
// Wait for the measurement to complete:
delay(status);

// Retrieve the completed pressure


measurement:
// Note that the measurement is stored in the
variable P.
// Note also that the function requires the
previous temperature measurement (T).
// (If temperature is stable, you can do one
temperature measurement for a number of pressure
measurements.)
// Function returns 1 if successful, 0 if
failure.

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");

// The pressure sensor returns abolute


pressure, which varies with altitude.
// To remove the effects of altitude, use
the sealevel function and your current altitude.
// This number is commonly used in weather
reports.
// Parameters: P = absolute pressure in mb,
ALTITUDE = current altitude in m.
// Result: p0 = sea-level compensated
pressure in mb

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

When you rotate the rotary encoder, it counts in the positive


direction and the reverse direction. Rotation counts are not limited.
With the buttons on the rotary encoder, you can reset to its initial
state and start counting from 0.
SAMPLE CODE:
int pinA = 3;
int pinB = 4;
int encoderPosCount = 0;
int pinALast;
int aVal;
boolean bCW;

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

This module can be used to trigger the effect of various vibration,


theft alarm, intelligent car, earthquake alarm, motorcycle alarm, etc.
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);
}
Speed Sensor Module

Tachometer is a simple module that allow you to test the speed of


the motor. Widely used for motor speed detection, pulse count,
position limit, and so on.
SAMPLE CODE:
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
int sensor = 11;
unsigned long start_time = 0;
unsigned long end_time = 0;
int steps=0;
float steps_old=0;
float temp=0;
float rps=0;

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

This module measures the acceleration. It’s commonly used in


portable devices and video game controllers to detect movement
and actions from the player.
SAMPLE CODE:
const int groundpin = 18;
const int powerpin = 19;
const int xpin = A3;
const int ypin = A2;
const int zpin = A1;

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

It’s a great little sensor to measure temperature and humidity in


your room or outside. It integrates seamlessly with the Arduino.
SAMPLE CODE:
#include <dht.h>
#define dht_apin A0
dht DHT;

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

Knock sensor are vibration sensors specifically designed to pick


up the vibration of knocking. Great for magic door openers
triggered by knocking on the door.
SAMPLE CODE:
int ledPin = 13;
int knockSensor = 0;
byte val = 0;
int statePin = LOW;
int THRESHOLD = 100;

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);
}

You might also like