HC-SR04 with Arduino Problem

I’m trying to connect Ultrasonic sensor with Cayenne. I’ll attach my code below… when i remove cayenne.begin(token) command sensor is working fine… if i add that sensor gives 0 as a output… i want to know what can i do for that…

//#define CAYENNE_DEBUG         // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneEthernet.h>
const int trigPin = 11;
const int echoPin = 10;
#define VIRTUAL_PIN V1


// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "73p5v7l6yb";

int maximumRange = 200; // Maximum range needed
int minimumRange = 0; // Minimum range needed
long duration, distance; // Duration used to calculate distance

void setup()
{
  Serial.begin(9600);
  Cayenne.begin(token);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop()
{
  Cayenne.run();


 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10); 
 
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;
 
 if (distance >= maximumRange || distance <= minimumRange){
 /* Send a negative number to computer and Turn LED ON 
 to indicate "out of range" */
 Serial.println("-1");
 }
 else {
 /* Send the distance to the computer using Serial protocol, and
 turn LED OFF to indicate successful reading. */
 Serial.println(distance);
 }
 
 //Delay 50ms before next reading.
 delay(50);  
}

CAYENNE_OUT(VIRTUAL_PIN)
{
  Cayenne.virtualWrite(VIRTUAL_PIN, distance);
}

@sameeramannapperuma welcome to cayenne community.
before going any further, swift to cayenne mqtt library as it more stable and recommended. for this add cayenne mqtt library and then try this code.

// This example shows how to connect to Cayenne using an Ethernet W5100 shield and send/receive sample data.

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTEthernet.h>
int Trigger = 5;
int Echo = 6;
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "MQTT_USERNAME";
char password[] = "MQTT_PASSWORD";
char clientID[] = "CLIENT_ID";

unsigned long lastMillis = 0;

void setup() {
    pinMode(Trigger, OUTPUT);
  pinMode(Echo, INPUT);
	Serial.begin(9600);
	Cayenne.begin(username, password, clientID);
}

void loop() {
	Cayenne.loop();
  long duration, distance;
  digitalWrite(Trigger, LOW);
  delayMicroseconds(2);

  digitalWrite(Trigger, HIGH);
  delayMicroseconds(10);

  digitalWrite(Trigger, LOW);
  duration = pulseIn(Echo, HIGH);
  distance = (duration / 2) / 29.1;

  Serial.print(distance);
  Serial.println("Centimeter:");
  delay(500);
	//Publish data every 10 seconds (10000 milliseconds). Change this value to publish at a different interval.
	if(millis() - lastMillis > 10000) {
		lastMillis = millis();
		//Write data to Cayenne here. This example just sends the current uptime in milliseconds.
		Cayenne.virtualWrite(0, distance);
	}
}
1 Like