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

1
/*
2
* ------------ [GP_v30_06] - Ultrasound sensor --------------
3
*
4
* Explanation: This is the basic code to manage and read the
5
* ultrasound sensor.Cycle time: 30 seconds
6
*
7
* Copyright (C) 2017 Libelium Comunicaciones Distribuidas S.L.
8
* http://www.libelium.com
9
*
10
* This program is free software: you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation, either version 3 of the License, or
13
* (at your option) any later version.
14
*
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
19
*
20
* You should have received a copy of the GNU General Public License
21
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22
*
23
* Version: 3.1
24
* Design: David Gascón
25
* Implementation: Alejandro Gállego
26
*/
27
28
#include <WaspSensorGas_Pro.h>
29
#include <UltrasoundSensor.h>
30
31
/*
32
* Waspmote OEM. Possibilities for this sensor:
33
* - CENTRAL SOCKET
34
* P&S! Possibilities for this sensor:
35
* - SOCKET_E
36
*/
37
38
uint16_t range;
39
40
void setup()
41
{
42
USB.println(F("Ultrasound sensor example"));
43
// Configures the I2C isolator
44
pinMode(GP_I2C_MAIN_EN, OUTPUT);
45
}
46
47
void loop()
48
{
49
///////////////////////////////////////////
50
// 1. Turn on the sensors
51
///////////////////////////////////////////
52
53
// Power on the sensor.
54
PWR.setSensorPower(SENS_3V3, SENS_ON);
55
// Enable the communication with the board
56
digitalWrite(GP_I2C_MAIN_EN, HIGH);
57
58
///////////////////////////////////////////
59
// 2. Read sensors
60
///////////////////////////////////////////
61
62
// Read the ultrasound sensor
63
range = Ultrasound.getDistance();
64
65
// And print the value via USB
66
USB.println(F("***************************************"));
67
USB.print(F("Distance: "));
68
USB.print(range);
69
USB.println(F(" cm"));
70
71
///////////////////////////////////////////
72
// 3. Power off sensors
73
///////////////////////////////////////////
74
75
// Disable the communication with the board
76
digitalWrite(GP_I2C_MAIN_EN, LOW);
77
// Power off the sensor.
78
PWR.setSensorPower(SENS_3V3, SENS_OFF);
79
80
///////////////////////////////////////////
81
// 4. Sleep
82
///////////////////////////////////////////
83
84
// Go to deepsleep
85
// After 30 seconds, Waspmote wakes up thanks to the RTC Alarm
86
PWR.deepSleep("00:00:00:30", RTC_OFFSET, RTC_ALM1_MODE1, ALL_OFF);
87
}
Copied!

Output

1
H#
2
Ultrasound sensor example
3
***************************************
4
Distance: 235 cm
5
***************************************
6
Distance: 241 cm
7
***************************************
8
Distance: 245 cm
9
***************************************
10
Distance: 249 cm
Copied!
Last modified 1yr ago