SCP 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 Smart Cities PRO board 1 x Ultrasound Sensor

Notes

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

Code

1
/*
2
------------ [SCP_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 <WaspSensorCities_PRO.h>
29
30
/*
31
Waspmote OEM. Possibilities for this sensor:
32
- SOCKET_1
33
- SOCKET_2
34
- SOCKET_3
35
- SOCKET_4
36
- SOCKET_5
37
P&S! Possibilities for this sensor:
38
- SOCKET_A
39
- SOCKET_B
40
- SOCKET_C
41
- SOCKET_E
42
- SOCKET_F
43
*/
44
ultrasoundCitiesSensor ultrasound(SOCKET_E);
45
46
// variable
47
uint16_t range;
48
49
void setup()
50
{
51
USB.println(F("Ultrasound sensor example"));
52
}
53
54
void loop()
55
{
56
///////////////////////////////////////////
57
// 1. Turn on the sensor
58
///////////////////////////////////////////
59
ultrasound.ON();
60
61
///////////////////////////////////////////
62
// 2. Read sensor
63
///////////////////////////////////////////
64
65
// Read the ultrasound sensor
66
range = ultrasound.getDistance();
67
68
// And print the value via USB
69
USB.println(F("***************************************"));
70
USB.print(F("Distance: "));
71
USB.print(range);
72
USB.println(F(" cm"));
73
74
///////////////////////////////////////////
75
// 3. Power off sensor
76
///////////////////////////////////////////
77
ultrasound.OFF();
78
79
///////////////////////////////////////////
80
// 4. Sleep
81
///////////////////////////////////////////
82
83
// Go to deepsleep
84
// After 30 seconds, Waspmote wakes up thanks to the RTC Alarm
85
USB.println(F("Enter deep sleep mode"));
86
PWR.deepSleep("00:00:00:30", RTC_OFFSET, RTC_ALM1_MODE1, ALL_OFF);
87
USB.ON();
88
USB.println(F("wake up!!"));
89
}
Copied!

Output

1
H#
2
Ultrasound sensor example
3
The sensor is placed in socket 4
4
***************************************
5
Distance: 112 cm
6
***************************************
7
Distance: 114 cm
8
***************************************
9
Distance: 111 cm
10
***************************************
11
Distance: 109 cm
Copied!
Last modified 1yr ago