Hello guys, I'm working on my college final project and I have to make a automatic cleaning robot and I have a kinda big problem.
For controlling the motors i used L293D on top of arduino, it has to do something with it?
I have 4x ultrasonic sensors HC-SR04 connected at digital pins 2,3,4,5, ECHO and TRIG are connected together to use less pins. But my readings are innacurate. Every once in a while I get 0 value and don't know what to do. I used bigger delay but nothing changes. Even if i disconnect other sensors the readings remain like this. I get values like this:
12
12
12
0
12
12
12
0
12
11
12
0
11
11
0
For reading the values i used ping_cm from newping library. Thank you!
Will you be posting the program so we can take a look, or would you like us to guess? At this point I'm not sure how we would guess at anything where 6 pins from unknown ultrasonic sensors connected to digital inputs produce any values other than 0 and 1.
The only serial output I see is sonar[0].ping_cm().
What are the digital pins 2, 3, 4, 5, ECHO, and TRIG (those two don't exist in the Arduino world, by the way, we need the pins numbers) connected to? You say the sensors, but the only time '2' appears is in reference to the motors and one of those includes '1'. which you can't be using pin 1, so that cannot be the pin number. So back to the question of what is connected to pins 2, 3, 4, 5? Where are ECHO and TRIG connected? What oes the function call "sonar[0].ping_cm()" do?
You should also post a wiring diagram and the ultrasonic sensor part numbers so it can be verified if you are wiring and using them correctly.
I have some mistakes in my writing, sorry. I'm from Romania and don't know how to explain properly in english.
TRIG and ECHO of the ultrasonic sensor HC-SR04 are connected together.
All the 4 sensors are conncted to digital pins 3, 4, 5, 6. Every pin from 3 to 6 is a TRIG and ECHO.
I used sonar[0].ping_cm() from NewPing library - Arduino Playground - NewPing Library sonar ping end a ping, returns the distance in inches or 0 (zero) if no ping echo within set distance limit
"something like this" is enough of a warning to not bother looking at the image. Post an image of YOUR setup as seemingly small details can make a world of difference.
For your code, reduce this to a minimal sketch that still shows the problem. In your case: a sketch that does nothing but read the sensors and display the values. Get that to work first. Then you can also see whether it's really the sensors that are the problem, or maybe some interaction with other parts of the code.
wvmarle:
"something like this" is enough of a warning to not bother looking at the image. Post an image of YOUR setup as seemingly small details can make a world of difference.
For your code, reduce this to a minimal sketch that still shows the problem. In your case: a sketch that does nothing but read the sensors and display the values. Get that to work first. Then you can also see whether it's really the sensors that are the problem, or maybe some interaction with other parts of the code.
I think i found the problem. L293D is controlled by digital pins 2,3,...,12 and i have the ultrasonic on pins 3-6 and i think this is the problem. I will try when i get home from work, if having more problems i will come with photos. Thanks!
Do you realise that this means the sensors will be retriggering themselves all the time? No wonder you get unexpected readings!
It's not just because that trig and echo are separate pins.
You CAN get away with connecting all trig pins together, and reading all echoes at the same time. As long as the sensors don't hear each other's signal. Then you need five pins for four sensors.
wvmarle:
Do you realise that this means the sensors will be retriggering themselves all the time? No wonder you get unexpected readings!
It's not just because that trig and echo are separate pins.
You CAN get away with connecting all trig pins together, and reading all echoes at the same time. As long as the sensors don't hear each other's signal. Then you need five pins for four sensors.
I don't have sensors to test with. I don't know the sensors the OP is using is the same as those used by the library author. Based on the library used, NewPing library - Arduino Playground - NewPing Library, the write up says the echo and trig pins can be the same pin. I don't know how it works, I didn't write the library and cannot test it; but that is what the library says.
The sensor triggers on a falling edge on the trig pin, regardless of how long trig has been high as long as it is at least 10 us. So that's exactly what happens when the echo is finished: instant re-trigger of the sensor.
That is, unless the firmware of the sensor ignores the trig pin for a short time in this situation.
It also requires the Arduino's I/O pin to be set as output to trigger, and as input to read the echo. The echo pin is normally held low, and goes high when a trig comes in. So unless that pin is set to input when the sensor is idle, you create a short the moment you try to trigger it by setting the Arduino pin high.
Doesn't matter anymore, thank you guys, finally i sort it out, ive read againd the data sheet for L293D shield and i saw that it uses all digital pins from 3 to 12 so... Now ive managed to get a constant reading at all the time as you can see: