Development

» GP v30 06 – Ultrasound sensor

This is the basic code to manage and read the ultrasound sensor.

Required Materials

1 x Waspmote
1 x Battery
1 x Gases PRO board v30
1 x Ultrasound Sensor

Notes

- Remember to connect the battery to Waspmote for proper operation.
- The connection of the sensor is described in the Gases PRO v30 technical guide.
- Cycle time: 30 seconds

Code

/*  
 *  ------------  [GP_v30_06] - Ultrasound sensor  -------------- 
 *  
 *  Explanation: This is the basic code to manage and read the 
 *  ultrasound sensor.Cycle time: 30 seconds
 *  
 *  Copyright (C) 2016 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:		    3.0
 *  Design:             David Gascón
 *  Implementation:     Alejandro Gállego
 */

#include <WaspSensorGas_Pro.h>
#include <UltrasoundSensor.h>

/*
 * Waspmote OEM. Possibilities for this sensor:
 * 	- CENTRAL SOCKET
 * P&S! Possibilities for this sensor:
 * 	- SOCKET_E
 */

uint16_t range;


void setup() 
{
    USB.println(F("Ultrasound sensor example"));
	// Configures the I2C isolator
	pinMode(I2C_MAIN_EN, OUTPUT);

  
}

void loop()
{
    ///////////////////////////////////////////
    // 1. Turn on the sensors
    /////////////////////////////////////////// 

    // Power on the sensor. 
	PWR.setSensorPower(SENS_3V3, SENS_ON);
	// Enable the communication with the board
	digitalWrite(I2C_MAIN_EN, HIGH);


    ///////////////////////////////////////////
    // 2. Read sensors
    ///////////////////////////////////////////  

    // Read the ultrasound sensor	
	range = Ultrasound.getDistance();

    // And print the value via USB
    USB.println(F("***************************************"));
    USB.print(F("Distance: "));
    USB.print(range);
    USB.println(F(" cm"));


    ///////////////////////////////////////////
    // 3. Power off sensors
    ///////////////////////////////////////////   

	// Disable the communication with the board
	digitalWrite(I2C_MAIN_EN, LOW);
    // Power off the sensor. 
	PWR.setSensorPower(SENS_3V3, SENS_OFF);


    ///////////////////////////////////////////
    // 4. Sleep
    /////////////////////////////////////////// 

    // Go to deepsleep	
    // After 30 seconds, Waspmote wakes up thanks to the RTC Alarm
    PWR.deepSleep("00:00:00:30", RTC_OFFSET, RTC_ALM1_MODE1, ALL_OFF);

}

Output

H#
Ultrasound sensor example
***************************************
Distance: 235 cm
***************************************
Distance: 241 cm
***************************************
Distance: 245 cm
***************************************
Distance: 249 cm

Quick Publish: