EN / USD
32
of 76
TEP , The Engineering Projects , Image

syedzainnasir

TEP , The Engineering Projects , Rating 7.5 7.5 / 10
TEP , The Engineering Projects , Icon Level: Moderator
TEP , The Engineering Projects , Icon Joined: 20 Mar 2022
TEP , The Engineering Projects , Icon Last Active: 2:21 PM
TEP , The Engineering Projects , Icon Location: TEP , The Engineering Projects , Flag
TEP , The ENgineering Projects , Icon TEP , The ENgineering Projects , Icon TEP , The ENgineering Projects , Icon TEP , The ENgineering Projects , Icon
Help with Multiple Ultrasonic Sensors
TEP , The Engineering Projects , Calender Question: 20-Feb-2017
TEP , The Engineering Projects , Category In: Arduino Projects
Hi guys,

I'm trying to create a robot using three HC-SR04 ultrasonic sensors and my Arduino Pro Mini but I've run into a few problems. In short the robot's function is as follows:

1. The robot is dual wheeled, with an H-bridge (SN754410) driving each wheel.
2. There's one HC-SR04 sensor on each side of the robot, the left one activates the left wheel motor when it detects a hand in front of it, vice versa for the right side.

i.e. To make the robot go forward, we place our hands near the left and right side of the robot, to make it turn right, we remove the right hand and keep the left one in place, vice versa for turning left, etc.

3. A third HC-SR04 is located the top of the robot, such that it activates a third motor when the user's hand is hovering above the robot.

My test code is as follows:
[code] #include <NewPing.h> #define SONAR_NUM 3 // Number of sensors. #define MAX_DISTANCE 20 // Maximum distance (in cm) to ping. NewPing sonar[SONAR_NUM] = { // Sensor object array. NewPing(4, 5, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping. NewPing(6, 7, MAX_DISTANCE), NewPing(8, 9, MAX_DISTANCE) }; #define ena1 10 //trigger for left motor H-bridge //#define ena2 11 //trigger for right motor //#define ena3 12 //for top motor long sensors[3]; //array to store sensor distances void setup() { Serial.begin (115200); pinMode(ena1, OUTPUT); //pinMode(ena2, OUTPUT); //pinMode(ena3, OUTPUT); } void loop() { for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results. delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings. sensors[i] = sonar[i].ping_cm(); } Serial.println(sensors[0]); if (sensors[0] > 0 && sensors[0] <= 20){ Serial.println("detected"); digitalWrite(ena1, HIGH); }else{ Serial.println("NA"); digitalWrite(ena1,LOW); } }[/code] As you can see, I'm using the NewPing.h library to collect the sensor data. After each iteration of the for loop, the distances detected by the sensors are stored in a sensor array. When a hand is placed about 15-20 cm away from a sensor, the arduino sends a digital "HIGH" trigger signal to the respective H-bridge, activating the respective motor (I only have one of these pins, "ena1", enabled in my code, the other two are commented for the test).

To test my code, I simply connected the H-bridge trigger pin "ena1" to an LED, this pin is activated by the sensor whose distance data is stored in variable "sensors[0]". However, after I compile and upload my code, I notice that the LED simply flickers faintly as I put my hand in front of the sensor. As if the LED is being turned on and off very fast.

The output from the serial monitor is as follows:
[code]15 detected 0 NA 16 detected 0 NA 14 detected 0 NA[/code] As you can see, by putting my hand about ~15cm in front of the sensor, the sensor returns the correct distance and the "ena1" pin is set to high (as evidenced by "detected" being printed to the screen).

However, the sensor always returns a "0" value at the next iteration of the main loop (while my hand is still in front of the sensor), subsequently setting the "ena1" pin to LOW again, which might explain why the LED is being turned on and off so fast.

I'm not sure why this is happening... Interestingly, by removing the digitalWrite lines from the code, the sensor returns the correct values (i.e. no "0" value when my hand is in front of the sensor).

Any ideas on how I can fix this?

Thanks in advance!
TEP , The Engineering Projects , Icon Answer: 1 TEP , The Engineering Projects , Icon Views: 150 TEP , The Engineering Projects , Icon Followers: 85
Small Bio
TEP , The Engineering Projects , Image

adiono

TEP , The Engineering Projects , Rating 7.5 7.5 / 10
TEP , The Engineering Projects , Icon Level: Moderator
TEP , The Engineering Projects , Icon Joined: 20 Mar 2022
TEP , The Engineering Projects , Icon Last Active: 2:21 PM
TEP , The Engineering Projects , Icon Location: TEP , The Engineering Projects , Flag
TEP , The ENgineering Projects , Icon TEP , The ENgineering Projects , Icon TEP , The ENgineering Projects , Icon TEP , The ENgineering Projects , Icon

RE:

Help with Multiple Ultrasonic Sensors
TEP , The Engineering Projects , Calender Comment: 20-Feb-2017
TEP , The Engineering Projects , Category In: Arduino Projects
[quote=Patricia post_id=119 time=1487599964 user_id=75] Hi guys,

I'm trying to create a robot using three HC-SR04 ultrasonic sensors and my Arduino Pro Mini but I've run into a few problems. In short the robot's function is as follows:

1. The robot is dual wheeled, with an H-bridge (SN754410) driving each wheel.
2. There's one HC-SR04 sensor on each side of the robot, the left one activates the left wheel motor when it detects a hand in front of it, vice versa for the right side.

i.e. To make the robot go forward, we place our hands near the left and right side of the robot, to make it turn right, we remove the right hand and keep the left one in place, vice versa for turning left, etc.

3. A third HC-SR04 is located the top of the robot, such that it activates a third motor when the user's hand is hovering above the robot.

My test code is as follows:
[code] #include <NewPing.h> #define SONAR_NUM 3 // Number of sensors. #define MAX_DISTANCE 20 // Maximum distance (in cm) to ping. NewPing sonar[SONAR_NUM] = { // Sensor object array. NewPing(4, 5, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping. NewPing(6, 7, MAX_DISTANCE), NewPing(8, 9, MAX_DISTANCE) }; #define ena1 10 //trigger for left motor H-bridge //#define ena2 11 //trigger for right motor //#define ena3 12 //for top motor long sensors[3]; //array to store sensor distances void setup() { Serial.begin (115200); pinMode(ena1, OUTPUT); //pinMode(ena2, OUTPUT); //pinMode(ena3, OUTPUT); } void loop() { for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results. delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings. sensors[i] = sonar[i].ping_cm(); } Serial.println(sensors[0]); if (sensors[0] > 0 && sensors[0] <= 20){ Serial.println("detected"); digitalWrite(ena1, HIGH); }else{ Serial.println("NA"); digitalWrite(ena1,LOW); } }[/code] As you can see, I'm using the NewPing.h library to collect the sensor data. After each iteration of the for loop, the distances detected by the sensors are stored in a sensor array. When a hand is placed about 15-20 cm away from a sensor, the arduino sends a digital "HIGH" trigger signal to the respective H-bridge, activating the respective motor (I only have one of these pins, "ena1", enabled in my code, the other two are commented for the test).

To test my code, I simply connected the H-bridge trigger pin "ena1" to an LED, this pin is activated by the sensor whose distance data is stored in variable "sensors[0]". However, after I compile and upload my code, I notice that the LED simply flickers faintly as I put my hand in front of the sensor. As if the LED is being turned on and off very fast.

The output from the serial monitor is as follows:
[code]15 detected 0 NA 16 detected 0 NA 14 detected 0 NA[/code] As you can see, by putting my hand about ~15cm in front of the sensor, the sensor returns the correct distance and the "ena1" pin is set to high (as evidenced by "detected" being printed to the screen).

However, the sensor always returns a "0" value at the next iteration of the main loop (while my hand is still in front of the sensor), subsequently setting the "ena1" pin to LOW again, which might explain why the LED is being turned on and off so fast.

I'm not sure why this is happening... Interestingly, by removing the digitalWrite lines from the code, the sensor returns the correct values (i.e. no "0" value when my hand is in front of the sensor).

Any ideas on how I can fix this?

Thanks in advance! [/quote]

Sometimes, to pinpoint problems, it helps to back up and simplify.

Start with the basic example from the library and gradually increase complexity.

For example, you could first see if you can get the simple NewPingExample for one sensor working properly with your Pro Mini, with no modifications to the example code (except the pins, serial speed, or delay). Remove all wiring except the minimum needed to get the sketch to work.

Then add your "if...else..." to the NewPingExample, but with only the Serial.Prints, and see if that works.

Then add the digitalWrites and led wiring, still with just one sensor and the NewPingExample.

Recommended Tutorial:
[url=http://www.theengineeringprojects.com/2015/02/interfacing-multiple-ultrasonic-sensor-arduino.html]INTERFACING OF MULTIPLE ULTRASONIC SENSOR WITH ARDUINO[/url]
TEP , The Engineering Projects , Tick Correct Answer
Comment Bio
TEP , The Engineering Projects , Tags
PLC
Robot
STM32
Arduino
AI
ESP32
Ladder Logic
PLC Projects
Programming
Communicates STM32
PLC Projects
Communicates PLC
Font Style
Alignment
Indenting and Lists
Insert Media
Insert Items

Want to leave an answer!

Word Count :0 Draft Saved at 12:42 am.