HC-SR04 Ultrasonic Sensor with Motor Control



I don’t know how to connect Dc motors with HC-SRO4 sensor. I need serious help here.



That depends on what you want to do. Most likely you went to feed the range finder into some logic that then feeds motor controller to either move our turn.

You could feed range finder into a map node to convert its output into 0-1 output that controls the speed of motor so robot moves forward until it reaches an obstacle (slowing as it approaches). Depending on range finder output, robot might not move if there is nothing within range…i think the built in range finder node returns 0 if it doesn’t see anything.



This what i tired and it working but it’s not precise as i want it to be !

with this programing it turn from one motor and and somehow avoids the obstacles but it get stuck in close corners. What i want is, it to get backwards and than take right or left and find its way out.

Tired map and given ranges to different ranges but it get confused. Further multiply sensor also giving same issues.



If you link the less node to Motor-2 Gate EN and use -0.3 to feed Motor-1 Gate instead of -0.5, robot should spin until it sees things are clear. If it hits the obstacle while spinning, then you need to increase the distance for the compare.

You probably want same value to compare for less & greater nodes. With current code, robot will just freeze if it detects a distance of 2400. Even better is just to feed less to a ‘not’ node, then feed that to Motor-1-2 Gate EN so you don’t need two compares.

1 Like


well, the above code is working fine for me and i have done few tests. But this code is very limited to detecting the obstacle and than spin and find its way out from arena. However, i want to make it more precise going backwards than right or left if not obstacle found go forward if not than than turn right does the same again it never stuck in the test.

My current code is only limited to one spin it does find its way out but how can i add further commands as i mentioned.




If you want to be able to perform a serious of actions based on input (found obstacle, back up, then search for escape), then you will need to start using pulses to control the program flow instead of tying sensors directly to the motor controllers with a little logic. A primer for how to do this can be found here: https://xod.io/docs/guide/simple-traffic-light/. You can go the next step in: gweimer/traffic-light-advanced. Load the latter by going to XOD and selecting File > Add Library and entering “gweimer/traffic-light-advanced”. I haven’t updated it lately…let me know if there are any issues with new XOD version.

Once you have gone through those tutorials and try to apply the concepts to your code, you will probably have more questions, but it will be a lot easier to help you if you already understand the concepts explained in the tutorials.

1 Like


Thank you, let me try this and than i will get back to you on.