I am wondering if it is possible to detect obstacles using the Analog and the Trig Pin maybe? If you can help with code it would be very helpfull.
Hi! Welcome to the Forum.
There are a lot of tutorials on the internet that shows how to use the HC-SR04 sensor, including connections and code. Among the first hits of Google there are these two:
Certainly you can find others in Youtube if you prefer videos.
Have fun!
Which Arduino board are you using?
Please post your formatted test code in a code block as described in the forum guidelines.
If I understood the question correctly, you can use analog pin as a digital input, just declare them using pinMode(A0, INPUT); but this only works on pins A0-A5. If you do not declare them like this, you can still use them using analogRead and interpret the returned values (0-1023) as you like. Also, on some boards some analog pins can be used as digital outputs (special pins, for example A0-A5 pins on Nano; you just need to declare them as output with for example pinMode(A0, OUTPUT);). Hope that helps. If you want code, you will have to post a wiring diagram.
All best, V
Nonsense.
If you use pins A0 to A5 as digital inputs (pinMode (A0, INPUT), or INPUT_PULLUP), they will behave exactly as any other digital input.
You can also use them as digital outputs (pinMode (A0, OUTPUT).
Avoid using A4 and A5 if you want to use I2C.
An HC-SR04 doesn't have an analogue pin.
You can connect the echo pin to A0 to A5 and use them as digital inputs, as noted above.
I did not formulate my previous post correctly, but I meant if you DO NOT declare them like this, you can hook up echo pin of HC-SR04 to them and use analogRead(). Then it will return either 0 or 1023 with HC-SR04. If you DO declare them like this, then they behave as you said. Sorry for the miscommunication.
That would be an exceptionally dumb thing to do.
Normally, we use a pulseIn on the echo pin.
That won't work with an analogRead, and even if it did, it would be around twenty five times slower, so would have similarly reduced precision.
OP, the NewPing library may be of interest.
That is correct, but if they only need it for obstacle detection and not distance measuring, then it would be fine (apart from being slower, but if you do not have an available digital pin (let's say for example the only remaining free pins are A6 and/or A7), it will do). When it comes to pulseIn, I avoid it as much as I can because it is a blocking function (similar to delay; blocks the rest of the code from executing while it lasts). I prefer to use interrupts (which will also only work with digital pins) when measuring distance. When it comes to it being dumb, I agree, but I only said that it is possible to be done, not that it should be done. There are several solutions, and other circumstances directly affect whether a given solution is correct or not. In order to best solve the problem I would need a wiring diagram, which is not provided.
A6 and A7 are the pins you use first for analogue-only purposes.
@anon56112670 you clearly did not read my whole post. I am talking about a situation when you need a digital input pin, but all digital pins (0-13 and A0-A5) are taken, you can improvise with using A6 or A7. This is a hypothetical situation. I have used HC-SR04 in the past, and in my opinion the best thing to do would be: connect trig to pin 4 (or some other pin that does not support interrupt, in order to keep interrupt pins free), echo to pin 3 (which does support interrupts), and use interrupts. But if those pins, and all other digital pins, are taken by other components (ie. there is not a single free digital pin on the board), you can improvise something like this with A6/A7. Of course something like this should be avoided. But it is possible.
Again we disagree - I'd prefer to use pin 8 (on a Uno or Nano)
But why? Pin 8 does not support interrupts (on Uno/Nano, only pins 2 and 3 support interrupts), which are a better method than pulseIn for handling HC-SR04. Are there other benefits to using pin 8?
Timer capture interrupt
No, they're the only pins that directly support external interrupts with their own vectors, but pin change interrupts are available on other pins.
Slightly more latency but very useful nonetheless.
Thanks, I did not know that!
Thank you so much
Thank you for wanting to have helped, but I found code to do the exact thing, but instead through Pin 2,3
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.