Arduino Sensors: Difference between revisions
(→HC-SR04: code) |
(→DS3231: code) |
||
Line 33: | Line 33: | ||
Source: [http://www.instructables.com/id/Real-time-clock-using-DS3231-EASY/step3/The-code/ instructables.com] |
Source: [http://www.instructables.com/id/Real-time-clock-using-DS3231-EASY/step3/The-code/ instructables.com] |
||
<pre style="width: 75%; height: 10pc; overflow-y: scroll;"> |
<pre style="width: 75%; height: 10pc; overflow-y: scroll;">#include <TM1637.h> |
||
#include <Wire.h> |
|||
#include <DS3231.h> |
|||
#include <config.h> |
|||
#define CLK 2 //Arduino pins which are connected to Display |
|||
#define DIO 3 |
|||
const byte gas_sensor = 0; |
|||
int gas_level; |
|||
int redLed = 12; |
|||
int buzzer = 10; |
|||
int sensorThres = 400; // Your threshold value |
|||
int i = 0; |
|||
TM1637 display(CLK,DIO); //To work with the chip clock and indicator we use the library |
|||
DS3231 rtc(SDA, SCL); // Init the DS3231 using the hardware interface |
|||
Time t; // Init a Time-data structure |
|||
void beeptone() |
|||
{ |
|||
for(i = 0; i < 255; i = i + 2) |
|||
{ |
|||
analogWrite(buzzer, i); |
|||
delay(10); |
|||
} |
|||
for(i = 255; i > 1; i = i - 2) |
|||
{ |
|||
analogWrite(buzzer, i); |
|||
delay(5); |
|||
} |
|||
for(i = 1; i <= 10; i++) |
|||
{ |
|||
analogWrite(buzzer, 200); |
|||
delay(100); |
|||
analogWrite(buzzer, 25); |
|||
delay(100); |
|||
} |
|||
} |
|||
void setup() |
|||
{ |
|||
// Serial.begin(9600); // Setup Serial connection |
|||
display.set(); //Enable and configure the indicator |
|||
display.init(); |
|||
rtc.begin(); // Initialize the rtc object |
|||
// The following lines can be uncommented to set the date and time |
|||
//rtc.setDOW(WEDNESDAY); // Set Day-of-Week to SUNDAY |
|||
//rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format) |
|||
//rtc.setDate(1, 1, 2014); // Set the date to January 1st, 2014 |
|||
} |
|||
void loop() |
|||
{ |
|||
int8_t timeDisp[4]; //The values to be displayed on each of 4 bits |
|||
t = rtc.getTime(); //Get data from the DS3231 |
|||
timeDisp[0] = t.hour / 10; //We receive dozens of hours using integer division |
|||
timeDisp[1] = t.hour % 10; //Unit obtain hours using modulo |
|||
timeDisp[2] = t.min / 10; //We do the same with minutes |
|||
timeDisp[3] = t.min % 10; |
|||
display.point(POINT_ON); //colon ON |
|||
display.display(timeDisp); //output it to the screen |
|||
delay (2000); |
|||
gas_level= analogRead(gas_sensor); //Gas Sensor get data |
|||
display.point(POINT_OFF); |
|||
display.DigitDisplayWrite(CLK,DIO,gas_level); |
|||
// Serial.println(gas_level); |
|||
if (gas_level > sensorThres) |
|||
{ |
|||
digitalWrite(redLed, HIGH); |
|||
beeptone(); |
|||
} |
|||
else |
|||
{ |
|||
digitalWrite(redLed, LOW); |
|||
noTone(buzzer); |
|||
} |
|||
delay (2000); |
|||
} |
|||
</pre> |
</pre> |
||
<br /> |
<br /> |
Revision as of 11:41, 6 November 2016
Here you will find a brief introduction and simple usage guide/instructions along with simple Sketch for arduino based sensors & addons like displays, motors, modules, etc.
This page is under construction. |
TM1637
- Connecting TM1637 4x7 Segment Display to Arduino
Source: tronixstuff.com
DS3231
- Setting Time for first usage
- DS3231 RTC Clock Using Non-Rechargeable Batteries
Source: raspberrypi-spy.co.uk, woodsgood.ca
- DS3231 RTC Clock with Display
Source: instructables.com
#include <TM1637.h> #include <Wire.h> #include <DS3231.h> #include <config.h> #define CLK 2 //Arduino pins which are connected to Display #define DIO 3 const byte gas_sensor = 0; int gas_level; int redLed = 12; int buzzer = 10; int sensorThres = 400; // Your threshold value int i = 0; TM1637 display(CLK,DIO); //To work with the chip clock and indicator we use the library DS3231 rtc(SDA, SCL); // Init the DS3231 using the hardware interface Time t; // Init a Time-data structure void beeptone() { for(i = 0; i < 255; i = i + 2) { analogWrite(buzzer, i); delay(10); } for(i = 255; i > 1; i = i - 2) { analogWrite(buzzer, i); delay(5); } for(i = 1; i <= 10; i++) { analogWrite(buzzer, 200); delay(100); analogWrite(buzzer, 25); delay(100); } } void setup() { // Serial.begin(9600); // Setup Serial connection display.set(); //Enable and configure the indicator display.init(); rtc.begin(); // Initialize the rtc object // The following lines can be uncommented to set the date and time //rtc.setDOW(WEDNESDAY); // Set Day-of-Week to SUNDAY //rtc.setTime(12, 0, 0); // Set the time to 12:00:00 (24hr format) //rtc.setDate(1, 1, 2014); // Set the date to January 1st, 2014 } void loop() { int8_t timeDisp[4]; //The values to be displayed on each of 4 bits t = rtc.getTime(); //Get data from the DS3231 timeDisp[0] = t.hour / 10; //We receive dozens of hours using integer division timeDisp[1] = t.hour % 10; //Unit obtain hours using modulo timeDisp[2] = t.min / 10; //We do the same with minutes timeDisp[3] = t.min % 10; display.point(POINT_ON); //colon ON display.display(timeDisp); //output it to the screen delay (2000); gas_level= analogRead(gas_sensor); //Gas Sensor get data display.point(POINT_OFF); display.DigitDisplayWrite(CLK,DIO,gas_level); // Serial.println(gas_level); if (gas_level > sensorThres) { digitalWrite(redLed, HIGH); beeptone(); } else { digitalWrite(redLed, LOW); noTone(buzzer); } delay (2000); }
- Library
Source: rinkydinkelectronics.com
HC-SR04
- HC-SR04 UltraSonic Distance Sensor with TM1637 Display
Source: codebender.cc
#include <Arduino.h> #include <TM1637Display.h> #include <NewPing.h> const byte TRIGGER_PIN = 11; const byte ECHO_PIN = 12; const int MAX_DISTANCE = 500; const byte CLK = 3; const byte DIO = 4; NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); TM1637Display display(CLK, DIO); void setup() { Serial.begin(115200); display.setBrightness(0x0f); } void loop() { delay(500); int x = sonar.ping_cm(); display.showNumberDec(x,false,3,1); Serial.print(x); Serial.println(" cm"); }
- Library
Source: arduino.cc
MQ-2 Gas Sensor
- If using the sensor for first time, leave it powered on for about 24-48 hours(Burn-in Time).
- Just calibrate it to get about 100-150 from analogRead in a good air condition.
- Normal air returns ~100-150
const int sensorPin= 0; const int buzzerPin= 11; int smoke_level; int powerLED = 13; int greenLED = 9; int redLED = 7; int threshold = 400; // Your threshold value int i = 0; void setup() { pinMode(sensorPin, INPUT); //the smoke sensor will be an input to the arduino pinMode(buzzerPin, OUTPUT); //the buzzer serves an output in the circuit pinMode(powerLED, OUTPUT); Serial.begin(9600); digitalWrite(buzzerPin, LOW); //keep buzzer silent on startup } void pulsePowerLED() { digitalWrite(powerLED, HIGH); delay(100); digitalWrite(powerLED, LOW); delay(100); } void alarm(){ for (int i=0; i<20; i++) { digitalWrite(powerLED, HIGH); digitalWrite(buzzerPin, HIGH); delay(500); digitalWrite(powerLED, LOW); digitalWrite(buzzerPin, LOW); delay(100); } } void loop() { if (millis() / 1000 / 20 < 1) //we need to give the device some time to stabilize { Serial.println("Calibrating for 20 seconds... "); while(millis() / 1000 / 20 < 1) { pulsePowerLED(); //pulse our power / blue LED so the user knows something is happening } Serial.println("Calibrated."); } smoke_level= analogRead(sensorPin); //arduino reads the value from the smoke sensor Serial.println(smoke_level); if(smoke_level > threshold) alarm(); else digitalWrite(buzzerPin, LOW); digitalWrite(powerLED, HIGH); delay(100); digitalWrite(powerLED, LOW); delay(2000); // Wait for 2 seconds }
DS18B20
Source: tweaking4all.com
#include <OneWire.h> #include <DallasTemperature.h> #include <TM1637Display.h> #define ONE_WIRE_BUS 7 OneWire oneWire(ONE_WIRE_BUS); // Setup to communicate with any OneWire device DallasTemperature sensors(&oneWire); #define CLK 3 #define DIO 2 TM1637Display display(CLK, DIO); void setup(void) { Serial.begin(9600); Serial.println("Dallas Temperature IC Control Library"); sensors.begin(); sensors.setResolution(12); display.setBrightness(0x0c); } void loop(void) { sensors.requestTemperatures(); // Send the command to get temperatures delay(1000); float temp = sensors.getTempCByIndex(0); int temp2 = temp * 100; display.showNumberDec(temp2); Serial.print("Temperature is: "); Serial.print(sensors.getTempCByIndex(0)); Serial.print(" C, "); }
JHD162A LCD
Source: rastating.com, circuitstoday.com
Pin 1 to GND Pin 2 to 5v Pin 3 to center pin of 10k ohm Pot(103), One leg to +VCC & other to GND Pin 4 to Arduino pin 12 Pin 5 to GND Pin 6 to Arduino pin 11 Pin 11 to Arduino pin 5 Pin 12 to Arduino pin 4 Pin 13 to Arduino pin 3 Pin 14 to Arduino pin 2 Pin 15 to 5v through a 220 ohm resistor Pin 16 to GND
Simple Text:
#include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 5, 4, 3, 2); int lastUpdate = 0; int currentIndex = 0; char* lyrics[] = { "Drink all the...", " BOOZE! ", "Hack all the... ", " THINGS! " }; void setup() { lcd.begin(16, 2); // LCD no of lines } void loop() { int time = millis(); if ((time - lastUpdate) >= 800) // print next line after 800ms { lcd.setCursor(0, 0); if (currentIndex == 0 || currentIndex == 2) { lcd.clear(); lcd.setCursor(0, 0); lcd.print(lyrics[currentIndex]); } else { lcd.setCursor(0, 1); lcd.print(lyrics[currentIndex]); } if (currentIndex == 3) // reset of increment index { currentIndex = 0; } else { currentIndex += 1; } lastUpdate = time; } }
Scrolling Text:
#include <LiquidCrystal.h> int pos=0; // variable to hold cursor position LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { lcd.begin(16, 2); //initializes 16x2 LCD lcd.print("Amandeep Singh & Mankirat Singh"); //text to display } void loop() { for(pos=0; pos<1; pos++) { lcd.scrollDisplayLeft(); //scrolls display left by two positions } delay(400); //sets the speed at which display moves }
PIR Sensor
Source: instructables.com
int calibrationTime = 30; //time given to sensor to calibrate long unsigned int lowIn; long unsigned int pause = 5000; boolean lockLow = true; boolean takeLowTime; int pirPin = 7; //Digital pin connected to the PIR sensor's output int ledPin = 8; void setup(){ Serial.begin(9600); pinMode(pirPin, INPUT); pinMode(ledPin, OUTPUT); digitalWrite(pirPin, LOW); Serial.print("calibrating sensor "); for(int i = 0; i < calibrationTime; i++){ Serial.print("."); delay(1000); } Serial.println(" done"); Serial.println("SENSOR ACTIVE"); delay(50); } void loop(){ if(digitalRead(pirPin) == HIGH){ digitalWrite(ledPin, HIGH); if(lockLow){ lockLow = false; Serial.println("---"); Serial.print("motion detected at "); Serial.print(millis()/1000); Serial.println(" sec"); delay(50); } takeLowTime = true; } if(digitalRead(pirPin) == LOW){ digitalWrite(ledPin, LOW); if(takeLowTime){ lowIn = millis(); takeLowTime = false; } if(!lockLow && millis() - lowIn > pause){ lockLow = true; Serial.print("motion ended at "); Serial.print((millis() - pause)/1000); Serial.println(" sec"); delay(50); } } }
Reed Switch
Source: electroschematics.com
int ledOpen=8; int ledClose=10; int switchReed=6; void setup(){ pinMode(ledOpen, OUTPUT); pinMode(ledClose, OUTPUT); pinMode(switchReed, INPUT); Serial.begin(9600); } void loop(){ if (digitalRead(switchReed)==HIGH){ digitalWrite(ledOpen, LOW); digitalWrite(ledClose, HIGH); Serial.println("Your Door is Closed"); } else { digitalWrite(ledOpen, HIGH); digitalWrite(ledClose, LOW); Serial.println("Your Door is Open"); } delay(1000); }
LDR
Source: diyhacking.com
This project will allow you to turn On or Off a LED using LDR.
You can adjust amount of light by adjusting the value of 'sensorValue'.
#include <SoftwareSerial.h> int sensorPin = A0; // select the input pin for the LDR int sensorValue; // variable to store the value coming from the sensor int led = 3; void setup() { pinMode(led, OUTPUT); Serial.begin(9600); } void loop() { sensorValue = analogRead(sensorPin); Serial.println(sensorValue); if(sensorValue < 300) { digitalWrite(led,HIGH); delay(1000); } else digitalWrite(led,LOW); delay(1000); }
Bluetooth Module
Source: instructables.com
#include <SoftwareSerial.h> SoftwareSerial SoftSerial(10, 11); // RX, TX int ledpin=13; int BluetoothData; void setup() { SoftSerial.begin(9600); SoftSerial.println("Bluetooth On please press 1 or 0 blink LED .."); pinMode(ledpin,OUTPUT); } void loop() { if (SoftSerial.available()){ BluetoothData=SoftSerial.read(); if(BluetoothData=='1'){ digitalWrite(ledpin,1); SoftSerial.println("LED On D13 ON ! "); } if (BluetoothData=='0'){ digitalWrite(ledpin,0); SoftSerial.println("LED On D13 Off ! "); } } delay(100); }
{{#widget:DISQUS |id=networkm |uniqid=Arduino Sensors |url=https://aman.awiki.org/wiki/Arduino_Sensors }}