This time we got rid of one Arduino and hooked a sensor directly to an XBee, while using the other XBee in API mode.
The idea: a noise sensor placed at a certain location is checking whether the sound is above a certain threshold, and if so, a message is displayed on an LCD that is placed at a different location, asking to lower the noise..
How to do it? A digital noise sensor is hooked to one of XBee's digital input pins(DIO0), which samples the data every 255ms and sends it to another XBee.
The other XBee is connected to an Arduino that has an LCD and that displays a message based on the input it receives.
We've encountered some problems with the Xbee reading the noise sensor and sending the data but in the end it seemed the problem was solved by connecting both the Xbee and the noise sensor to a 5-volt power source instead of 3.3V, and inverting the readings(1 for no noise, 0 for noise).
For this lab the task was to create a sunset sensor that with the use of a photocell you could light some LEDs depending on the time of day, or the light acting on the cell. We have taking the idea a step forward by creating an alarm system that utilizes two sensors, the photocell and the ultrasonic range finder. Both sensors are connect to an Arduino UNO that is configured to send bytes to another Arduino UNO. In order to achieve the stream of bytes the Xbee on the Arduino has to be configured to Coordinator API. The scenario for this project is to keep a valuable object safe from others in a secret place. We therefore set the object inside a closet where the photocell will be calibrated to the light once the door is closed thus the inside, where the sensor is located with the object, is completely dark. A change of the state of the sensor will happen once the door is opened. Since an intruder can open the door with the outside also dark the sensor will not set off the alarm because there is no change in the light. Due to this issue, the ultrasound sensor is used to determine a change on the position of the object, that we want to keep away of the hand of the enemy. If any of the sensors notice a change the alarm (buzzer) will go off and a tune will play. The material used:
(2) Computers
(2) Arduino UNO
(2) XBee explorer (and at least one USB-to-mini-USB cable)
(2) XBee
(2) Breadboards
(1) Green LED
(1) Piezo speaker (buzzer)
(1) Photocell
(1) Ultrasonic Range Finder
(1) Button
(2) 10kΩ
In order to simplefy it we start with one part of the whole system, the one with the sensors. The following sketch shows all the connections and materials used.
The finnised set up looks like this with the choosen object to be kept save:
The code for the Arduino on this set-up with the sensors:
#define lightPin 0 //define a pin for Photo resistor
#define trigPin 12 //define a pin for the US trigger
#define echoPin 11 //define a pin for the US echo (our input)
//int audioPin = 12; //define a pin for LED
int LDRreading, outputValue, LDRMax = 0, LDRMin = 1023;
String incomingBytes;
boolean Running = false, calibrated = false;
//variables needed for reading distance from US sensor
long duration, distance, durationMax = 0, durationMin = 100000;
void setup()
{
Serial.begin(9600); //Begin serial communcation
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop()
{
while (Serial.available() > 0) {
delay(10); //small delay to allow input buffer to fill
char c = Serial.read(); //gets one byte from serial buffer
incomingBytes += c; //create the word we're receiving
}
//Serial.println(incomingBytes);
if (incomingBytes.startsWith("stop"))
Running = false;
if (incomingBytes.startsWith("start")) {
Running = true;
//if (!calibrated)
calibrate();
}
incomingBytes = "";
if (Running == true) {
LDRreading = analogRead(lightPin); //read the value of the photoresistor
// Serial.print("LED reading is: ");
// Serial.println(LDRreading); //Write the value of the photoresistor to the serial monitor.
//start measuring distance
digitalWrite(trigPin, LOW); //toggle the pin OFF so we're sure we'll have a clean reading
delayMicroseconds(2); //short delay
digitalWrite(trigPin, HIGH); //10 microseconds ON time, that's all the Ultrasound sensor needs
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); //we read the duration of the sound wave bouncing back from the object
distance = (duration / 2) / 29.1; //we divide it by 2 as the sound wave needs to get to the object and back and again by 29.1 to get centimeters
For the second lab it was time to add the wirelessness in the game. In doing that, we used the wireless technology called ZigBee (or 802.15.4), and more specifically we used 2 XBees from Digi, which takes care of some higher layers in the protocol, so we can focus on developing the applications.
After installing the X-CTU software and the VCP drivers for FTDI on the computer(in order to configurate the XBees), we set the PAN ID to "ABCD" and configured one as Coordinator, one as Router and set the Destination Adresses to one another so they could communicate. We tested the connection and it didn't work at first because probably there was another PAN with the same ID, so we changed it to "BBBB" (amazingly no one thought about that). After that, we tried several possible applications with another group, managed to send a multi-hop message, but in the end we changed the idea to creating a wireless mini-piano. Thus,we connected one XBee the computer and the other one to the Arduino as shown in the schematic below, added a buzzer on PIN 12 and coded the application. What the application does is send characters from the keyboard of the computer through the USB serial connection to the Xbee, which in turn forwards the data to the Xbee connected to the Arduino, and the Arduino plays different sounds based on the characters it receives. The Arduino also gives feedback to the Xbee so we can see what the Arduino received in the serial connection on the computer. We coded 2 octaves on the characters QWERTYUIOP{}| and `1234567890-=, "q" is middle C, "w" is C#, "e" is D and so on.
For the first lab in WSN we got to play with an LED, and since it was feeling lonely we started to play with an ultrasound sensor too.
The initial idea was to build an application that could provide audio feedback for the distance of nearby objects, but we decided to change it to visual feedback(varying brightness in LEDs) as it was a lab about blinking LEDs.
Having figured out how blinking the LED worked we started playing with the ultrasound(US) sensor and quickly got it running as well. We then connected both together.
Because we wanted to show different brightness depending on the distance of an object, we had to change the LED's input to pin 6 on the Arduino from the previous pin 13 used in order to control its voltage output. Digital pin 6 uses pulse width modulation(PWM) which mimics an analog output by giving pulses of various duration, making variable brightness possible.
At the end we also added a button to start or pause our application.
The final result is an application that detects when objects are 20 cms or less from the sensor and slowly increases the LED's brightness accordingly.
Materials used:
An LED
A ultrasound sensor HC-SR04
Arduino
Breadboard
PushButton
2.2kΩ resistor
Photo:
Video:
Wiring:
Arduino Sketch code:
/* HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 11 Trig to Arduino pin 12
Red POS to Arduino pin 11
Green POS to Arduino pin 10
560 ohm resistor to both LED NEG and GRD power rail
More info at: http://goo.gl/kJ8Gl
* Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
* press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's
* a minimum delay between toggles to debounce the circuit (i.e. to ignore
* noise).
*/
#define trigPin 11
#define echoPin 12
#define led 6
#define inPin 2
int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
//variables needed for reading distance from US sensor
long duration, distance;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(inPin, INPUT);
pinMode(led, OUTPUT);
//pinMode(led2, OUTPUT);
}
void loop() {
reading = digitalRead(inPin);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
time = millis();
}
if(state == HIGH) {
// //turn LED ON
// analogWrite(led,distance);
//start measuring distance
digitalWrite(trigPin, LOW); //toggle the pin OFF so we're sure we'll have a clean reading
delayMicroseconds(2); //short delay
digitalWrite(trigPin, HIGH); //10 microseconds ON time, that's all the Ultrasound sensor needs
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); //we read the duration of the sound wave bouncing back from the object
distance = (duration/2) / 29.1; //we divide it by 2 as the sound wave needs to get to the object and back and again by 29.1 to get centimeters