Click here to Skip to main content
15,037,168 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to connect two ultrasonic sensors(HCSR-04) and a motor to raspberry pi 3. Here, i want to control the motor by detecting the distance measured by the two sensors.

1.First , on startup, my motor should run on constant speed. 2.On detection of the obstacle(Eg. above 50cm), then the motor should automatically slow down. 3.The second sensor is used as a parking sensor which alerts the driver only.

I know how to connect the two sensors, but I am unable to interface them with motor for speed control. I want the code for above in python. Plz help.

What I have tried:

Don't know about how to control motor with sensor's output
Updated 16-Aug-17 2:43am

1 solution

I'm sorry, but no one here is writing code on request.

We can help if you got stuck with some existing code.

To help you a little bit:

You need the programmatic interfaces to the sensors and the motor (read sensor data and write commands to the motor) and must implement a regulation algorithm.

For the first you need example code (if it exists), or you have to implement it based on the datasheets and the used communication (I2C, SPI, serial, direct IO port access; whatever applies).

For the algorithm you have to define the regulation rules and then implement them in code. This can be done finally only by you because fine tuning of the rules (or at least the regulation parameters) usually requires testing them with the hardware.

Perform the required tasks step by step. Start for examples with the sensors and write a program that displays the values. Then write a program that controls the motor by passing appropriate control values (e.g. on the command line). Once you can control the hardware, start thinking about the regulation algorithm and implementation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900