Development

Sensors / Smart Cities Board:

» SC 08: Ultrasound Sensor Reading

In this example the Smart Cities sensor board is turned on and the ultrasound sensor read every three seconds approximately, printing the result through the USB port.

Required Materials

1 x Waspmote
1 x Battery
1 x Coin cell
1 x Smart Cities board
1 x Ultrasound sensor (XL-MaxSonar-WRA1)

Notes

* Remember to connect the battery to Waspmote for proper operation.
* The connection of the sensor is described in the Smart Cities technical guide.
* Take into account that when powering the temperature sensor you will be turning on also the humidity, luminosity and dust sensors.
* This code has been created to read the ultrasound sensor on the temperature socket, to use it on the humidity socket replace SENS_CITIES_ULTRASOUND_3V3 by SENS_CITIES_ULTRASOUND_5V in the setSensorMode and readValue functions.
* To use the LV-MaxSonar-EZ0 sensor replace SENS_US_WRA1 by SENS_US_EZ0 in the readValue call.

Code

/*  
 *  --[SC_8] - Reading the Ultrasound Sensor on Smart Cities board-- 
 *  
 *  Explanation: Turn on the sensor every three seconds, taking a measurement and
 *               printing its result through the USB port.
 *  
 *  Copyright (C) 2012 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:           0.1 
 *  Design:            David Gascón 
 *  Implementation:    Manuel Calahorra
 */

#include <WaspSensorCities.h>

// Variable to store the read value
float value;

void setup()
{
  // Turn on the USB and print a start message
  USB.ON();
  USB.println(F("start"));
  delay(100);

  // Turn on the sensor board
  SensorCities.ON();
  
  // Turn on the RTC
  RTC.ON();
  
}
 
void loop()
{
  // Part 1: Sensor reading
  // Turn on the sensor and wait for stabilization and response time
  SensorCities.setSensorMode(SENS_ON, SENS_CITIES_ULTRASOUND_3V3);
  delay(2000);
  
  // Read the ultrasound sensor 
  value = SensorCities.readValue(SENS_CITIES_ULTRASOUND_3V3, SENS_US_WRA1);
  
  // Turn off the sensor
  SensorCities.setSensorMode(SENS_OFF, SENS_CITIES_ULTRASOUND_3V3);
  
  // Part 2: USB printing
  // Print the distance read value through the USB
  USB.print(F("Distance: "));
  USB.print(value);
  USB.println(F("cm"));
  
  delay(1000);
}

Output

B#
start
Distance: 178.4274139404cm
Distance: 178.4274139404cm
Distance: 26.2096767425cm
Distance: 178.4274139404cm

Quick Publish: