Ultrasonic sensor (HC - SR04) for output different Colors on LED

Hi all,

I'm new on this topic and hoping someone is going to get me out of trouble :sweat_smile:

I want to use the HC - SR04 sensor to measure different distances. Let's say: everything below 10 and 10 or higher. When the distance is below 10 the red LED has to be turned on. When it's higher as 10 the green LED has to light up. I'm working with the Node MCU.

See attached code how my progress is going. Hopefully someone is be able to help me out.

// defines pins numbers
const int trigPin = 1;
const int echoPin = 2;
const int RED = 5;
const int GREEN = 6;
const int BLUE = 7;

// defines variables
long duration;
int distance;
int safetyDistance;


void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
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;

safetyDistance = distance;
if (safetyDistance <= 10){
  analogWrite(RED, 255);
  analogWrite(GREEN, 0);
  analogWrite(BLUE,0);
}

else{
  analogWrite(RED, 0);
  analogWrite(GREEN, 255);
  analogWrite(BLUE,0);
}

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

Sooo, what exactly is your question?

What pins are used by the Serial port?

All I guess? Or do I have to put the serial after the triggerman and echopin in my setup?

LightuC:
Sooo, what exactly is your question?

Sorry if was not clear enough. My question is: the code which I have now is not working for me. I think I'm very close but don't know what I do wrong at this point.

stumper:
Sorry if was not clear enough. My question is: the code which I have now is not working for me. I think I'm very close but don't know what I do wrong at this point.

And what exactly is it, that is not working?

const int trigPin = 1;

I have not used NodeMCU, but it looks to me like pin 1 is the Serial TX pin so a different pin needs to be used for the HCSR04 trigger. Do you get believable data from the HCSR04?

LightuC:
And what exactly is it, that is not working?

Well that's the whole point, I don't understand why it's not working

He's asking you to be more descriptive of HOW it's not working. Is the green light on all the time? Are both on all the time? No lights ever? Is there smoke and/or fire? What do you see in the serial monitor? Etc etc.

It would also help if you could take a picture or draw out how you have everything wired.

stumper:
Well that's the whole point, I don't understand why it's not working

The question was not why isn't it working. It was what is not working. What exactly does the program do when you run it and what should it be doing that is different.

You see we don't all have the same equipment that you have so we can't really test it for you. You have to do the testing and report back on what happens.

Steve

Ah thanks, it seems so obvious now.

Luckily I don't see any smoke....

After uploading the code, the green led is on, but it's not very bright. After several seconds both the green and blue led turned on at full brightness. In the serial monitor nothing appears, unfortunately.

I've attached some pictures with my setup, the green led and both green and blue led's.

That's very strange, I copied your code exactly and I was at least getting data on the serial monitor - I even tried hooking the sensor up wrong and it at least said "Distance: 0" over and over. The focus should be on getting that sensor working first of all. Try different pins like groundfungus said. Just don't forget to change them at the very top of your code to whatever pins you try. Also make sure you have your baud rate set to 9600 in the serial monitor.

As for the blue light showing up, I'm guessing you are using the Elegoo manual? It looks like they have the pinout wrong on the RGB LED, lol. I hooked mine up and got the same blue light! Just change Red = 5 and Blue = 7 and that should be fixed.

Other than that the code seems to be doing what you want it to do on my end - turns red and stays red when I'm within 10cm, goes back to green if it's over 10cm. I do want to point out that this part:

// Calculating the distance
distance= duration*0.034/2;

safetyDistance = distance;
if (safetyDistance <= 10){

seems weird to me. safetyDistance has no real function here. I get where you were going but then you want to set it equal to 10 at the top when you define it by saying

int safetyDistance = 10;

and then you'd change the if statement to be

if (distance <= safetyDistance) {
//do stuff
}

But hey, it seems like it works as is too!

Thanks for your help! I've updated my code following the steps you recommend. Now the next problem showed up... I get the following error when uploading:

error: espcomm_upload_mem failed

My code:

// defines pins numbers
const int trigPin = 8;
const int echoPin = 4;
const int Red = 5;
const int Green = 6;
const int Blue = 7;

// defines variables
long duration;
int distance;
int safetyDistance = 10;


void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(Red, OUTPUT);
pinMode(Green, OUTPUT);
pinMode(Blue, OUTPUT);
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;

safetyDistance = distance;
if (distance <= safetyDistance){
  analogWrite(Red, 255);
  analogWrite(Green, 0);
  analogWrite(Blue,0);
}

else {
  analogWrite(Red, 0);
  analogWrite(Green, 255);
  analogWrite(Blue,0);
}

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

I've never used that board, so I'm not sure why it would say that. Googling the problem seems to be related to the esp2866 built in - are you uploading code wirelessly or with the usb plugged in? Try USB if you're using wireless.

In Arduino IDE, press tools and make sure that you have selections under "Board", "Processor", and "Port" that make sense. Then try and do Tools -> Get Board Info and make sure it's getting that. Sometimes I have issues uploading and just unplug it and plug it back in, press reset on the board, etc until it goes through. I actually found several people that said they were getting that error, but the code was still being loaded and running fine. Make sure and check if that's happening by putting your hand in front of the sensor and seeing if the light changes.

I was very busy with working so had no time to test this earlier. Luckily I've fixed the error message so I can upload codes again, as you said it was a matter of changing the ports. Now when I upload I see a green bright light for several seconds and I was very excited, thought it was working but then it switched to a blue light. I checked the serial monitor and their is still no input.

When I upload another code I got the same reaction: green bright light and after that a blue one.

My code:

// defines pins numbers
const int trigPin = 5;
const int echoPin = 6;
const int Red = 2;
const int Green = 3;
const int Blue = 4;

// defines variables
long duration;
int distance;
int safetyDistance = 10;


void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  pinMode(Red, OUTPUT);
  pinMode(Green, OUTPUT);
  pinMode(Blue, OUTPUT);
  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;

  safetyDistance = distance;
  if (distance <= safetyDistance) {
    analogWrite(Red, 255);
    analogWrite(Green, 0);
    analogWrite(Blue, 0);
  }

  else {
    analogWrite(Red, 0);
    analogWrite(Green, 255);
    analogWrite(Blue, 0);
  }

  // Prints the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.println(distance);
}

Replace your setup

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
  Serial.println (F("Ready..."));
}

Do you see anything now?

Nope I still don't see a thing. When I obtain the board info it shows me this:

BN: Unknown board
VID: 10C4
PID: EA60
SN: Upload any sketch to obtain it

Maybe it has to do something with this?

Or I did the setup wrong:

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
  Serial.println (F("Ready..."));

  pinMode(Red, OUTPUT);
  pinMode(Green, OUTPUT);
  pinMode(Blue, OUTPUT);
}

Are you running Windows, Linux or mac?

When you run the code with the setup() posted by AWOL do you see the Ready... message in the serial monitor?

I'm running on a Mac.

And I don't see anything in serial monitor.