0

I want to check my understanding on how to use digitalWrite for 5V and Grd and why this code isn't working.

This is for the Makeblock sensor

Consider the image here which says to plugin a wire to 5V, Grd and a signal pin.

My code does the following

#define h  22
#define l  24
#define sig 26

void setup() {
   Serial.begin(9600);
   pinModde(h, OUTPUT);
   pindMode(l, OUPUT);
   pinMode(sig, OUPUT);
   
   digitalWrite(h, HIGH);
   digitalWrite(l, LOW);
}

void loop() {
   digitalWrite(sig, LOW);
   delayMicroseconds(2);
   digitalWrite(sig, HIGH);
   delayMicroseconds(10);
   digitalWrite(sig, LOW);
   pinMode(sig, INPUT);
   long duration = pulseIn(sig, HIGH, 38000);
   delay(2500);
}

When I run this code duration is always 0. If I unplug 22 and 24 and use the board's 5V and Grd I get duration is 30001 no matter how far away an object is from the sensor. Meaning it's not sensing anything, just returning this number.

Full disclosure that I'm using solder-less wires but I don't that would make this much of a difference?

So to summarize: why are 22 and 24 resulting in a 0 readout and not acting as 5V and Grd? My only thought is maybe too much voltage and a resister is needed?

Secondly: why is the sensor not working even when using 5V and Grd pins?

1

You've set pinMode(sig,INPUT); and never changed this back to OUTPUT before triggering next measure:

void loop() {
   pinMode(sig, OUTPUT);    // ensure sig is configured as an output
   digitalWrite(sig, LOW);
   delayMicroseconds(2);
   digitalWrite(sig, HIGH);
   delayMicroseconds(10);
   digitalWrite(sig, LOW);
   pinMode(sig, INPUT);
   long duration = pulseIn(sig, HIGH, 38000);
   delay(2500);
}

And for example PING))) (which is basically the same module) has power consumption about 30mA-35mA in datasheet. Output pin might be strong enough to power it, but I'd recommend at least GND connect directly to arduino GND. And using transistor for powering it up would be even better

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.