My Cart

How to Measure Sea Swell with an Accelerometer and LEDs using Waspmote Starter Kit

Difficulty Level: Intermediate -

1. Introduction

This tutorial allows the user to know the sea state thanks to the Waspmote accelerometer and turn ON and OFF several LEDs depending on the sea swell. If there are big waves, all LEDs will turn ON and a buzzer will sound. It also sends all the read data via XBee to a PC with a XBee gateway.

Ingredients:

  • 1 x Waspmote Starter Kit:
    • 1x Waspmote 802.15.4 uFL
    • 1x Waspmote Gateway 802.15.4 uFL
    • 1x 2300 mAh LiPo Battery
    • 1x miniUSB Cable
  • 1 x Starter Kit:
    • 6x LEDs
    • 6x 220Ω resistors
    • 1x Buzzer
    • 1x Breadboard
    • 1x Programming cable
    • Jumper Wires

Preparation Time: 30 minutes

Buy now

For further information about Waspmote, consult the main tutorial.

Step 1: Connection

Connect the LEDs with the resistors and the buzzer in the breadboard as you can see in the next diagram.

Connect two wires, red and black, to the two long rows on the side of the breadboard to provide access to 3.3V in and ground.

Connect digital pin 7 to the positive terminal of the buzzer and the other leg to GND.

Connect digital pins 1 to 6 to the longest leg of each LED. Place a resistor in the other leg of the LED and the remaining leg of the resistor to GND.

Finally connect the XBee in the correct socket as you can see in the diagram and plug the XBee gateway in your PC.

Step 2: The Code

Waspmote:

Code:
/*  
 *  Waspmote Starter Kit
 *  
 *  Copyright (C) Libelium Comunicaciones Distribuidas S.L. 
 *  http://www.libelium.com 
 *  
 *  This program is free software: you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation, either version 3 of the License, or 
 *  (at your option) any later version. 
 *  
 *  This program is distributed in the hope that it will be useful, 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License 
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *  
 *  Version:           1.0
 *  Design:            David GascĂłn 
 *  Implementation:    Marcos Martinez & Victor Boria
 */

#include <WaspXBee802.h>

////////////////////////////////////////////////
// XBee Parameters
////////////////////////////////////////////////

// Destination MAC address
char* MAC_ADDRESS="0013A200400A3451";

packetXBee* packet; 
char pac[100];

////////////////////////////////////////////////
// Acceleration offset threshold declaration
////////////////////////////////////////////////

#define offset1 100
#define offset2 200
#define offset3 300
#define offset4 400
#define offset5 500
#define offset6 600

////////////////////////////////////////////////
// Flag declaration 
////////////////////////////////////////////////

int i = 0;

void setup() {
	
  ////////////////////////////////////////////////
  // 0. Initialization
  ////////////////////////////////////////////////
  
  // 0.1 Clear string
  memset(pac,0,sizeof(pac));
  
  // 0.2 Turn ON xbee802
  xbee802.ON();
  
  // 0.3 Turn ON acclerometer
  ACC.ON();
  
  // 0.4 USB port for debugging
  USB.ON();
  USB.println(F("Ahoy! This is MareMote"));
  delay(100);

  // 0.5 Sending init XBee message
  snprintf(pac,sizeof(pac),"Ahoy! This is MareMote\n");
  sendXbee(pac);

  ////////////////////////////////////////////////
  // 1. Digital pins configuration
  ////////////////////////////////////////////////
  
  pinMode(DIGITAL1,OUTPUT);  //Green LED
  pinMode(DIGITAL2,OUTPUT);  //Green LED
  pinMode(DIGITAL3,OUTPUT);  //Green LED
  pinMode(DIGITAL4,OUTPUT);  //Green LED
  pinMode(DIGITAL5,OUTPUT);  //Red LED
  pinMode(DIGITAL6,OUTPUT);  //Red LED
  pinMode(DIGITAL7,OUTPUT);  //Buzzer
  digitalWrite(DIGITAL7,LOW);
  
  ////////////////////////////////////////////////
  // 2. Low power mode
  ////////////////////////////////////////////////

  USB.println("Sleep mode");
  
  snprintf(pac,sizeof(pac),"Sleep mode\n");
  sendXbee(pac);
  
  // 2.1 Accelerometer interrupt
  ACC.setIWU(); 

  // 2.2 Sleep mode
  PWR.sleep(ALL_OFF);

}


void loop() {

  ////////////////////////////////////////////////
  // 3. Acceleration measurement
  ////////////////////////////////////////////////
  
  // 3.1 Turn ON accelerometer
  ACC.ON();
  // 3.1 Turn ON xbee802
  xbee802.ON();
  
  // 3.1 Check accelerometer
  byte check = ACC.check();

  // 3.4 Z axis measure
  int z_acc = ACC.getZ();

  // 3.5 USB debugging
  USB.print(F("WaveMeter:\t")); 
  USB.println(z_acc, DEC);

  // 3.6 Sending data via XBee
  snprintf(pac,sizeof(pac),"WaveMeter: %i\n",z_acc);
  sendXbee(pac);

  ////////////////////////////////////////////////
  // 4. Check acceleration level
  ////////////////////////////////////////////////
  
  if (z_acc > 950 && z_acc < 1050){

    digitalWrite(DIGITAL1, LOW);
    digitalWrite(DIGITAL2, LOW);
    digitalWrite(DIGITAL3, LOW);
    digitalWrite(DIGITAL4, LOW);
    digitalWrite(DIGITAL5, LOW);
    digitalWrite(DIGITAL6, LOW);
    digitalWrite(DIGITAL7,LOW);  

    i++;  

  }

  if (((z_acc > 950 - offset1) && z_acc < 950) || ((z_acc < 1050 + offset1) && z_acc > 1050)){

    digitalWrite(DIGITAL1, HIGH);
    digitalWrite(DIGITAL2, LOW);
    digitalWrite(DIGITAL3, LOW);
    digitalWrite(DIGITAL4, LOW);
    digitalWrite(DIGITAL5, LOW);
    digitalWrite(DIGITAL6, LOW);
    digitalWrite(DIGITAL7,LOW);

    i = 0;    

  }

  if (((z_acc > (950 - offset2)) && z_acc < (950 - offset1)) || ((z_acc < (1050 + offset2)) && z_acc > (1050 + offset1))){

    digitalWrite(DIGITAL1, HIGH);
    digitalWrite(DIGITAL2, HIGH);
    digitalWrite(DIGITAL3, LOW);
    digitalWrite(DIGITAL4, LOW);
    digitalWrite(DIGITAL5, LOW);
    digitalWrite(DIGITAL6, LOW);   
    digitalWrite(DIGITAL7,LOW); 

    i = 0;

  }

  if (((z_acc > (950 - offset3)) && z_acc < (950 - offset2)) || ((z_acc < (1050 + offset3)) && z_acc > (1050 + offset2))){

    digitalWrite(DIGITAL1, HIGH);
    digitalWrite(DIGITAL2, HIGH);
    digitalWrite(DIGITAL3, HIGH);
    digitalWrite(DIGITAL4, LOW);
    digitalWrite(DIGITAL5, LOW);
    digitalWrite(DIGITAL6, LOW); 
    digitalWrite(DIGITAL7,LOW); 

    i = 0;  

  }

  if (((z_acc > (950 - offset4)) && z_acc < (950 - offset3)) || ((z_acc < (1050 + offset4)) && z_acc > (1050 + offset3))){

    digitalWrite(DIGITAL1, HIGH);
    digitalWrite(DIGITAL2, HIGH);
    digitalWrite(DIGITAL3, HIGH);
    digitalWrite(DIGITAL4, HIGH);
    digitalWrite(DIGITAL5, LOW);
    digitalWrite(DIGITAL6, LOW);
    digitalWrite(DIGITAL7,LOW); 

    i = 0;   

  }

  if (((z_acc > (950 - offset5)) && z_acc < (950 - offset4)) || ((z_acc < (1050 + offset5)) && z_acc > (1050 + offset4))){

    digitalWrite(DIGITAL1, HIGH);
    digitalWrite(DIGITAL2, HIGH);
    digitalWrite(DIGITAL3, HIGH);
    digitalWrite(DIGITAL4, HIGH);
    digitalWrite(DIGITAL5, HIGH);
    digitalWrite(DIGITAL6, LOW);
    digitalWrite(DIGITAL7,LOW);

    i = 0;    

  }

  if (z_acc < (950 - offset6) || z_acc > (1050 + offset6)){

    digitalWrite(DIGITAL1, HIGH);
    digitalWrite(DIGITAL2, HIGH);
    digitalWrite(DIGITAL3, HIGH);
    digitalWrite(DIGITAL4, HIGH);
    digitalWrite(DIGITAL5, HIGH);
    digitalWrite(DIGITAL6, HIGH);    
    digitalWrite(DIGITAL7,HIGH);

    i = 0;

  }


  // 4.1 Quiet mode 
  if (i >150){  

    USB.println("Calm sea-Sleep mode");
    snprintf(pac,sizeof(pac),"Calm sea-Sleep mode\n");
    sendXbee(pac);
    
    i = 0; 
    
    // 4.2 Clear interruption vector
    clearIntFlag(); 
    PWR.clearInterruptionPin();

    // 4.3 Accelerometer interrupt
    ACC.setIWU(); 
    
    // 4.4 Sleep mode
    PWR.sleep(ALL_OFF);

  }
}

////////////////////////////////////////////////
// Send XBee function
////////////////////////////////////////////////

void sendXbee(char* str){

  // Memory allocation
  packet=(packetXBee*) calloc(1,sizeof(packetXBee)); 
  
  // Choose transmission mode: UNICAST or BROADCAST
  packet->mode=BROADCAST; 
  
  // set destination XBee parameters to packet
  xbee802.setDestinationParams( packet, MAC_ADDRESS, str, MAC_TYPE);
  
  // send XBee packet
  xbee802.sendXBee(packet);
  
  // free variables
  free(packet);
  packet=NULL;
  
}


        

Links and Documentation

Related Tutorials

Datasheets and Manuals



If you are interested in Internet of Things (IoT) or M2M projects check our open source sensor platform Waspmote which counts with more than 100 sensors available to use 'off the shelf', a complete API with hundreds of ready to use codes and a low consumption mode of just 0.7µA to ensure years of battery life.

Know more at:

Get the Starter Kits at: