Connecting a distance sensor with servo motor

Hello guys!
I want to connect a distance ultrasonic sensor with a servo motor : when the sensor detect a movement less than 100cm the servo turn on 175 degrees and when it detect nothing the servo motor turn back to his initial position.
i hope you help me really soon and thank you !

Sounds like you've got a clear idea of what you want. What's the question?

Look at the IDE-provided Example code for driving a Servo, and mix that with the code from the Example on reading Ultrasonic sensors. The example code is really good.

Only thing you might want to think about is whether you have a small enough servo that you can drive it directly from the microcontroller GPIO pin, or whether your servo needs more current than the microcontroller can source, in which case your circuit design needs to include a servo driver chip.

1 Like

Pick a servo and pick a sensor. You will find examples for both in the IDE and libraries.

You might want to consider the loop logic. The simple implementation just keeps checking the distance and adjusting the servo. This results in the servo getting repeat commands continuously like:
go 175
go 175
go 175...

Instead, you can remember the state of the servo, and only change it when the input changes.