So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By Tremor
#72666 Hello, first post here so hopefully I'm not breaking any rules!

I purchased a NodeMCU, and an Arduino starter kit to play with over my Christmas break and I'm trying to use the NodeMCU with an Ultrasonic Sensor(HC-SR04) but for some reason the sensor always reads zero for distance(when hooked up to the Arduino it reads properly so I know its not busted). I am fairly new to all of this but neither my own code nor the code from the guide I used to test worked. I have attached an image of my setup but just to clarify I have

VCC > 5V(Power supply)
GND > GND
Trig > D1
Echo > D2

The NodeMCU(Amica, actually a V2 board not V1 like the image) is being powered via USB to my Macbook and the Power Supply is powered via wall plugin.
I have 5V coming out of the power supply as I've tested it with my multimeter.(have also tested with a 3.3V supply as recommended online)

My code is as follows
Code: Select all// defines pins numbers

const int trigPin = 5;  //D1
const int echoPin = 4;  //D2

// defines variables
long duration;
int distance;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}

void loop() {

// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
delay(2000);
}


ImageNodeMCU-US by carter clackson, on Flickr

If you have ever had this issue or see something glaringly wrong with my code please let me know! I can't seem to solve why it reads zero through my NodeMCU and correctly on my Uno.
User avatar
By Tremor
#72667 I actually partially solved the problem in the last few hours. I am now getting data from the Ultrasonic sensor to the NodeMCU, but it seems as though ever 6-7 times it loops I get a nonsense value of between 0-5cm when it should be around 70cm. Is this just the nature of the sensor or is there something else going on?

Code: Select all// defines pins numbers

const int trigPin = 5;  //D1
const int echoPin = 4;  //D2

// defines variables
long duration;
long distance;


void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}

void loop() {
int distance = sample_distance();
Serial.println("Distance: ");
Serial.println(distance);
delay(2000);
}


int sample_distance() {
  long duration, distance;

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

  digitalWrite(trigPin, HIGH);
  delayMicroseconds(30);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  Serial.println("Duration: ");
  Serial.println(duration);
  distance = int((duration / 2) / 29.155);
  return distance;
}


Is my code now
User avatar
By Tremor
#72679 I have it hooked up to 5v. The image of the power supply just isn't the greatest. I have tested with a multimeter and its 5v. I got it working now but I get alot of garbage values, I will get 6-7 real distances in a row and then 3-4 that are like 0-4cm for no reason.