Skip to content

Commit d9a9410

Browse files
authored
Merge pull request #27 from shramik49/master
Add HC-SR04 code
2 parents baf7fb9 + 5e9a308 commit d9a9410

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

examples/CommunitySubmitted/DHT/DHT.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
3. Attach a DHT to your Arduino:
1010
Schematic:
1111
DHT Arduino
12-
[VDD] --- [3V3]
12+
[VDD] --- [5V]
1313
[GND] --- [GND]
1414
[OUTPUT] --- [Digital Pin 2]
1515
4. Set the TEMPERATURE_VIRTUAL_CHANNEL value below to a free virtual channel.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Cayenne HC-SR04 Example
3+
This sketch shows how to send HC-SR04 Sensor data to the Cayenne Dashboard.
4+
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
5+
Steps:
6+
1. Attach a HC-SR04 to your Arduino:
7+
Schematic:
8+
HC-SR04 Arduino
9+
[VCC] --- [5V]
10+
[GND] --- [GND]
11+
[TRIGGER_PIN] --- [Digital Pin 2]
12+
[ECHO_PIN] --- [Digital Pin 3]
13+
2. Set the DISTANCE_VIRTUAL_CHANNEL value below to a free virtual channel.
14+
3. Set the Cayenne authentication info to match the authentication info from the Dashboard.
15+
4. Compile and upload this sketch.
16+
5. Once the Arduino connects to the Dashboard it should automatically create temporary display widget with data.
17+
To make a temporary widget permanent click the plus sign on the widget.
18+
*/
19+
20+
#define CAYENNE_DEBUG
21+
#define CAYENNE_PRINT Serial // Comment this out to disable prints and save space
22+
#include <CayenneMQTTEthernet.h>
23+
24+
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
25+
char username[] = "MQTT_USERNAME";
26+
char password[] = "MQTT_PASSWORD";
27+
char clientID[] = "CLIENT_ID";
28+
29+
#define DISTANCE_VIRTUAL_CHANNEL 1
30+
31+
// Defines pins numbers for the HC-SR04 connections.
32+
#define TRIGGER_PIN 2
33+
#define ECHO_PIN 3
34+
35+
// Defines variables for storing the calculated values.
36+
long duration;
37+
int distance;
38+
39+
void setup() {
40+
pinMode(TRIGGER_PIN, OUTPUT);
41+
pinMode(ECHO_PIN, INPUT);
42+
Serial.begin(9600);
43+
Cayenne.begin(username, password, clientID);
44+
}
45+
46+
void loop() {
47+
Cayenne.loop();
48+
}
49+
50+
// This function is called at intervals to send HC-SR04 sensor data to Cayenne.
51+
CAYENNE_OUT(DISTANCE_VIRTUAL_CHANNEL)
52+
{
53+
digitalWrite(TRIGGER_PIN, LOW); // Clears the trigger pin
54+
delayMicroseconds(2);
55+
56+
digitalWrite(TRIGGER_PIN, HIGH); // Sets the trigger pin on HIGH state for 10 micro seconds
57+
delayMicroseconds(10);
58+
digitalWrite(TRIGGER_PIN, LOW);
59+
60+
duration = pulseIn(ECHO_PIN, HIGH); // Reads the echo pin, returns the sound wave travel time in microseconds
61+
62+
distance = duration * 0.034 / 2; // Calculating the distance.
63+
64+
// Send the distance value to Cayenne on proximity widget in centimeter.
65+
Cayenne.virtualWrite(DISTANCE_VIRTUAL_CHANNEL, distance, "prox", "cm");
66+
}

0 commit comments

Comments
 (0)