.
3-axis accelerometer motion detector project Reviewed by Unknown on 01:35 Rating: 4.5

3-axis accelerometer motion detector project

Description:
A 3-axis accelerometer sits at the heart of this project to provide a nifty little motion detector. Want to know who is stealing from the cookie jar? Want a simple home intrusion detector? Or to test your partner's driving skills? Then have a look at this:

(Soldering required for this project)


Video:




Parts Required:


Instructions:

  1. Overlay the Seeed Studio Base Shield onto the Freetronics Eleven (or compatible Arduino).
  2. Use a Universal Cable to attach a Seeed Studio Grove Button to Analog Pin 0 on the Base Shield. The socket is located directly above the Freetronics Eleven Power plug, and next to the Reset button on the Base Shield. Please note that Analog Pin 1 is not used by the Grove Button.
  3. Use a universal Cable to attache a Seeed Studio Grove Buzzer to Analog Pin 1 on the Base Shield. This is the socket next to the one used in Step 2.
  4. Solder the female header pins to the Protoboard. Overlay the protoboard onto the Base Shield to create a third layer. I created this layer to tidy up the project and make it a little bit more portable. You could just wire up another breadboard on the side.
  5. Stick a mini-breadboard (4.5cm x 3.5cm) onto the protoboard. This allows you to use the protoboard for other projects.
  6. Solder the male headers to the 3-axis accelerometer, and then place it centrally onto the breadboard.
  7. You need 5 wires to connect:
    • GND on protoboard to GND on accelerometer
    • 5V on protoboard to     VIN on accelerometer
    • Analog Pin 3 on protoboard to X on accelerometer
    • Analog Pin 4 on protoboard to Y on accelerometer
    • Analog Pin 5 on protoboard to Z on accelerometer
  8. Connect digital pin 8 to an LED and 330 ohm resistor on the breadboard,
  9. Use a wire to connect the resistor mentioned above to GND on the protoboard
  10. Connect the USB cable from your computer to the Freetronics Eleven, and upload the Arduino Sketch to the board. 
  11. Disconnect the USB cable, and then power the Freetronics Eleven using a 9V battery and clip.
  12. When you press the button, it will sound 3 warning sounds before it becomes activated.
  13. If it detects a vibration or motion that exceeds the tolerance level, it will alarm. The alarm will continue until you either press the Grove button - which resets and reactivates the device or you can press the Reset button on the Base Shield to Stop monitoring for motion.


Sketch







Freetronics Eleven / Arduino Sketch:

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//Motion Detector Alarm - Written by ScottC on 2/08/2012

//Global Variables and constants
const int buttonPin = A0; // button Pin connected to Analog 0
const int buzzerPin = A1; // buzzer Pin connected to Analog 1


//Accelerometer Pins
const int x = A3; // X pin connected to Analog 3
const int y = A4; // Y pin connected to Analog 4
const int z = A5; // Z pin connected to Analog 5


//Alarm LED
const int ledPin = 8; // LED connected to Digital 8



int tolerance=20; // Sensitivity of the Alarm
boolean calibrated=false; // When accelerometer is calibrated - changes to true
boolean moveDetected=false; // When motion is detected - changes to true



//Accelerometer limits
int xMin; //Minimum x Value
int xMax; //Maximum x Value
int xVal; //Current x Value

int yMin; //Minimum y Value
int yMax; //Maximum y Value
int yVal; //Current y Value

int zMin; //Minimum z Value
int zMax; //Maximum z Value
int zVal; //Current z Value



void setup(){
//Begin Serial communication
Serial.begin(38400);

//Initilise LED Pin
pinMode(ledPin, OUTPUT);

}



void loop(){
// If the button is pressed, initialise and recalibrate the Accelerometer limits.
if(analogRead(buttonPin)>500){
calibrateAccel();
}

// Once the accelerometer is calibrated - check for movement
if(calibrated){
if(checkMotion()){
moveDetected=true;
}
}

// If motion is detected - sound the alarm !
if(moveDetected){
Serial.println("ALARM");
ALARM();
delay(1000);
}

}





//This is the function used to sound the buzzer
void buzz(int reps, int rate){
for(int i=0; i<reps; i++){
analogWrite(buzzerPin,900);
delay(100);
analogWrite(buzzerPin,0);
delay(rate);
}
}




// Function used to calibrate the Accelerometer
void calibrateAccel(){
// reset alarm
moveDetected=false;

//initialise x,y,z variables
xVal = analogRead(x);
xMin = xVal;
xMax = xVal;

yVal = analogRead(y);
yMin = yVal;
yMax = yVal;

zVal = analogRead(z);
zMin = zVal;
zMax = zVal;

// Calibration sequence initialisation sound - 3 seconds before calibration begins
buzz(3,1000);

//calibrate the Accelerometer (should take about 0.5 seconds)
for (int i=0; i<50; i++){
// Calibrate X Values
xVal = analogRead(x);
if(xVal>xMax){
xMax=xVal;
}else if (xVal < xMin){
xMin=xVal;
}

// Calibrate Y Values
yVal = analogRead(y);
if(yVal>yMax){
yMax=yVal;
}else if (yVal < yMin){
yMin=yVal;
}

// Calibrate Z Values
zVal = analogRead(z);
if(zVal>zMax){
zMax=zVal;
}else if (zVal < zMin){
zMin=zVal;
}

//Delay 10msec between readings
delay(10);
}

//End of calibration sequence sound. ARMED.
buzz(3,40);
printValues(); //Only useful when connected to computer- using serial monitor.
calibrated=true;

}



//Function used to detect motion. Tolerance variable adjusts the sensitivity of movement detected.
boolean checkMotion(){
boolean tempB=false;
xVal = analogRead(x);
yVal = analogRead(y);
zVal = analogRead(z);

if(xVal >(xMax+tolerance)||xVal < (xMin-tolerance)){
tempB=true;
Serial.print("X Failed = ");
Serial.println(xVal);
}

if(yVal >(yMax+tolerance)||yVal < (yMin-tolerance)){
tempB=true;
Serial.print("Y Failed = ");
Serial.println(yVal);
}

if(zVal >(zMax+tolerance)||zVal < (zMin-tolerance)){
tempB=true;
Serial.print("Z Failed = ");
Serial.println(zVal);
}

return tempB;
}





// Prints the Sensor limits identified during Accelerometer calibration.
// Prints to the Serial monitor.
void printValues(){
Serial.print("xMin=");
Serial.print(xMin);
Serial.print(", xMax=");
Serial.print(xMax);
Serial.println();

Serial.print("yMin=");
Serial.print(yMin);
Serial.print(", yMax=");
Serial.print(yMax);
Serial.println();

Serial.print("zMin=");
Serial.print(zMin);
Serial.print(", zMax=");
Serial.print(zMax);
Serial.println();

Serial.println("------------------------");
}




//Function used to make the alarm sound, and blink the LED.
void ALARM(){

//don't check for movement until recalibrated again
calibrated=false;

// sound the alarm and blink LED
digitalWrite(ledPin, HIGH);
buzz(4,20);
digitalWrite(ledPin, LOW);
}
Description: 3-axis accelerometer motion detector project Rating: 3.5 Reviewer: Unknown ItemReviewed: 3-axis accelerometer motion detector project
Grove OLED 96x96 Slideshow Reviewed by Unknown on 00:12 Rating: 4.5

Grove OLED 96x96 Slideshow

This project makes use of the Grove OLED 96x96 display to present a mini-slideshow.  Pictures on your computer are transferred to the OLED via a Processing script, and will cycle through them as many times as you choose.

Video:



Parts required:


Software required:


Sketch:
















Arduino Sketch:
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// OLED Slideshow: Arduino Sketch written by ScottC 21/07/2012

#include <Wire.h>
#include <SeeedGrayOLED.h> //From http://garden.seeedstudio.com/images/c/c4/SeeedGrayOLED.zip
#include <avr/pgmspace.h>

int counter=0;

void setup()
{
//Allow communication to OLED
Wire.begin();

//Allow Serial communication between Freetronics Eleven and Computer
Serial.begin(28800);

//Initialise the OLED
SeeedGrayOled.init();
//Clear the OLED display
SeeedGrayOled.clearDisplay();
//Set to vertical mode - horizontal mode doesn't work with this example
SeeedGrayOled.setVerticalMode();

}

void loop(){
//Listen for Serial comunication
while (Serial.available()>0) {
// Read data and send directly to the OLED
sendMyData(Serial.read());
counter++;

//When counter reaches 4608 pixels, the picture is complete.
if(counter>4607){
//Insert delay to allow viewing of picture.
delay(4000);
Serial.println("End of Transmission");

//Reset the counter for the next picture
counter=0;
}
}
}

// This function was adapted from the SEEED Gray OLED driver so that
// character bytes could be sent directly to the OLED.
void sendMyData(unsigned char Data){
Wire.beginTransmission(SeeedGrayOLED_Address); // begin I2C transmission
Wire.send(SeeedGrayOLED_Data_Mode); // data mode
Wire.send(Data);
Wire.endTransmission();
}


// This function was adapted from the SEEED Gray OLED driver so that
// commands could be sent directly to the OLED.
// NOT USED IN THIS EXAMPLE ***********************
void sendMyCommand(unsigned char Cmd){
Wire.beginTransmission(SeeedGrayOLED_Address); // begin I2C communication
Wire.send(SeeedGrayOLED_Command_Mode); // Set OLED Command mode
Wire.send(Cmd);
Wire.endTransmission(); // End I2C communication
}




Processing Sketch:
  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* OLED Slideshow: Processing Sketch Written by ScottC 21/7/2012 

References:
Getting Pixels: http://www.learningprocessing.com/examples/chapter-15/example-15-7/
Greyscale conversion = http://www.openprocessing.org/sketch/60336
*/


import processing.serial.*; /* Needed for Serial Communication */

/* Global variables */
Serial comPort;
String [] comPortList;
String comPortString;
PImage img;
char[] tempGrey=new char[4609];
int startOffset=0;
ArrayList picNames;
int curLoop=1;
int totalPics=0;
int curPicNum=0;
boolean toggleSend=true;
boolean sendBouncer=true;

//Change maxLoops to a number > 1 if you want the pictures to loop.
int maxLoops=1;


void setup() {
//The size of the display is critical (must match the OLED)
size(96, 96);
//setup Serial
comPortList = Serial.list();
if(comPortList.length>0){
//baud rates greater than 28800 may produce unexpected results
comPort = new Serial(this, comPortList[0], 28800);
comPort.bufferUntil('\n');
} else{
println("NO COM PORTS AVAILABLE");
}

//Create an Array of pictures
picNames=new ArrayList();
picNames.add("Picture1.bmp");
picNames.add("Picture2.bmp");
picNames.add("Picture3.bmp");
picNames.add("Picture4.bmp");
// for more pictures just keep adding them to the list.
//The actual pictures must be located in the data folder of this project.
//Select Sketch/Add File to add the files to this folder.
//Make sure that the name of pictures match the names above.

//Get the total number of pictures added
totalPics=picNames.size();
}

void draw(){
if(toggleSend && sendBouncer){

// Debugging code: print("STARTED:");
// Debugging code: println(picNames.get(curPicNum));

sendImage((String)picNames.get(curPicNum)); //Send the picture to the OLED
toggleSend=false; //temporarily stop sending any more pictures until authorised
curPicNum++; //increment in preparation for the next picture

if(curPicNum==totalPics){
curPicNum=0; //go back to the first picture
curLoop++; //increment the loop counter
}
if(curLoop>maxLoops){
sendBouncer=false; //Stop any further looping
println("ANIMATION COMPLETE");
}
}
}


void sendImage(String imgName){
img = loadImage(imgName);
image(img,0,0,width,height);
loadPixels();
int counter=0;
for (int x = 0; x < width; x=x+2) {
for (int y = 0; y < height; y++) {
counter++;
int PixLoc = x + y*height; // this reads down then across2.
//Left pixel nibble
int Pix1=(round((red(pixels[PixLoc])*0.222+green(pixels[PixLoc])*0.707+blue(pixels[PixLoc])*0.071)))/16;
//Right pixel nibble
int Pix2=(round((red(pixels[PixLoc+1])*0.222+green(pixels[PixLoc+1])*0.707+blue(pixels[PixLoc+1])*0.071)))/16;
//Shift the byte <<4 for the left pixel nibble
int PixShift1=Pix1<<4;
//Combine both nibbles to form a byte
int PixFin = PixShift1+Pix2;
byte PixByteFin=byte(PixFin);
//Assign this byte to the tempGrey array
tempGrey[counter] = char(PixByteFin);
}
}
sendSerial(tempGrey); //Send the image data through the Serial COM Port/
}


//This function will send the byte/Char array to the Freetronics
//Eleven or Arduino.
void sendSerial(char[] Data){
for(int i=0; i<4608; i++){
//Needs an offset to get picture to align to screen properly
//only needs to do this once.
if(startOffset==0){
i=i+6;
startOffset++;
}
//Send the picture data to the Freetronics Eleven / Arduino
comPort.write(Data[i]);
}
}


//This function will wait for a response from the Freetronics
//Eleven or Arduino before sending any further pictures.
void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\n');
if (inString != null) {
println(inString);
toggleSend=true; // Allow the next picture to be sent
}
}


Please note: that you must use the Arduino IDE version 023 until Seeed Studio update their driver for this OLED. Their current driver is not compatible with later versions of Arduino IDE.


Description: Grove OLED 96x96 Slideshow Rating: 3.5 Reviewer: Unknown ItemReviewed: Grove OLED 96x96 Slideshow
Back to top