student project help - proximity or IR sensor to control motor

Hello,

I am new to the site and pretty much brand new to Arduino, except for taking a couple classes at the local maker shop.

I am a student I am trying to get something done for a presentation. This is part of an idea and I was hoping to use an Arduino to show it.

I have searched a lot but I am not sure I am getting the correct information for exactly what I want to try to do, so I figured I would just ask.

I would like to use a proximity sensor to control a motor (stepper or servo). So as something gets closer the motor turns but has a limit to how far it will go. I would also be interest in (just not my first choice but would like to compare) just a IR as the switch, again with a motor location limit

If anyone could direct me to the code and/or a diagram, parts that would be wonderful.

Thank you for any help.
Matthew

matthew_turner:
I would like to use a proximity sensor to control a motor (.....servo).

That's exactly what this guy did with his automated Pez dispenser.

JimboZA:
That's exactly what this guy did with his automated Pez dispenser.

Thanks.

I am probably completely wrong but isn't the power of a Ultrasonic Distance Sensor HC-SR04 is that you can control the amount based on the distance from it, not just start and stop?

At least that is what I was hoping for. Control the throw of the motor with the distance from the sensor.

If so, is there a benefit to use this sensor instead of a IR switch for just on/off?

Thanks again!

That sensor gives you the actual distance, yep, not just "near" vs " not near".

Whether or not that's a benefit to you, only you can say: sounds like you want to position something based on the actual distance and you could certainly do that.

JimboZA:
That sensor gives you the actual distance, yep, not just "near" vs " not near".

Whether or not that's a benefit to you, only you can say: sounds like you want to position something based on the actual distance and you could certainly do that.

Ideally, yes, it would. Do you have any recommendations?

Thanks

Recommendations for what, the sensor?

There are IR distance (as distinct from near/not-near) sensors too, such as the Sharp GP2Y0A21. That’s a simple analog output, ie it delivers a voltage which you read on an analog pin, and the graph shows what that means as a distance.

You would need to compare sensors according to whatever your criteria might be: physical size, cost, range?

If you mean recommendations for code, well first I’d say you need to work through some of the tutorials if you didn’t already. Especially, look at the servo examples there. Get a servo that looks about right. Unless the thing you want to move is particularly heavy, you’ll probably find a hobby servo will do. Get either a micro sized on like in the Pez example, or a standard one, and hook it up.

Then decide on a sensor and hook it up. Get it to show the distance in the serial monitor. There’s tons of code around for that, just google “arduino” along with whatever servo model it is, “arduino GP2Y0A21” say.

Get a servo that looks about right. Unless the thing you want to move is particularly heavy, you’ll probably find a hobby servo will do. Get either a micro sized on like in the Pez example, or a standard one, and hook it up.

Then comes the fun part… get the distance to become a servo angle. For that you’ll need to read up on if/else and give it ranges, like if distance is > blah1 but < blah2, servo angle is yadda. Get to that later though.

Time to get your feet wet… get your hands on a sensor and a servo and off you go.