Ultrasonic sensor with stepper motor

Hi, I'm not good in English but I want to ask help for my research project. My project is door controlled by ultrasonic sensor. I need a code for ultrasonic sensor with stepper motor. It starts from open. When sensor detect an object the door will close and stop, and if sensor does not detect anything, the door will open and stop.

I'm using an Arduino Uno, nema 17 stepper motor and L298n motor driver.
Please help me for code

I would suggest adapting a couple basic sketches to work with your hardware, such as :

and

https://create.arduino.cc/projecthub/Isaac100/getting-started-with-the-hc-sr04-ultrasonic-sensor-036380

and then combine them with the logic in the state change detection.

If you can make the parts work separately, that solves lots of problems. Then adjust the connections/pin numbers so they work attached to the same board at the same time with different sketches, that solves the next level of problems. Then you need to work out the logic to make them interact properly within one sketch.

To add to @DaveX 's suggestion, you should get each one working by itself then when you have all them working you can combine them into your final sketch.

1 Like

@rommel_27

We can better help if we know what is your native language?
We can better help if we know what is your mother-language?

Be the change you want to see in the world
best regards Stefan

Try writing the instructions is plain (your language), for example;

Wenn der Objektabstand 3 m beträgt, drehen Sie den Stepper um 100 Grad.

That would take two functions: Detecting the object distance and turning the stepper (in the right direction).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.