How to turn geared DC motor to specified angle with arduino

Hello every one ..
I try to make a solar tracker that tracks the sun through sun-earth relationships , it is several equations in which we find the sun position. The last two equations give us two angles , these angles called azimuth and elevation . these relationships are well described in the following link
http://pveducation.org/pvcdrom/properties-of-sunlight/suns-position

the question is : the equations result an angles , how to turn geared dc motor to specified angle with arduino.
thanks to every one who try to help me :slight_smile:

You need some sort of feedback from the tracker to tell the current angle from a reference plane. An optical encoder or potentiometer on each axis. Unless you use a stepper motor. Position is known by the number of steps as long as the motor never stalls.

Ok , if I put a potentiometer for feedback , but how to convert a number (angle) to physical output i.e. turns the dc motor to match specified angle .

If I understood correctly, you'll have to (physically) attach the potentiometer to the angle you want to measure.

Position 0 of the potentiometer will be set to angle 0, and position MAX will be set to the maximum angle you want to measure.

Then on the arduino, you'll just have to read the analogue power value and cross compute the corresponding angle: the lowest current will be angle 0, maximum current max angle, and (certainly) linearly between.

hope that helps

Kevin

thank you guys .. really appreciate your help. :slight_smile:

What kind of resolution do you need in the measurement? Azimuth 0° to 180° , elevation 0° to 90°? In degrees or tenths of a degree? If degrees a pot may be enough. Be aware that most pots are 270°, full sweep, so you loose some resolution if directly coupled to the output shaft (90° is 341 ADC counts out of 1024). If tenths or better an optical encoder on the Motor shaft, before gearing, can greatly improve resolution.

thank U groundfungus . :smiley:

Also - if you do decide to use a potentiometer to monitor the angle of the panel, make sure it is spec'd as a "linear taper", and -not- "audio taper" or "log taper" - otherwise your readings across the range will be very "weird" toward one end of travel (well - they'll be logarithmic, actually). Also - make sure it is a 10k ohm potentiometer.

Basically, to do what you want to do - from the point of view of using a potentiometer - is fairly simple:

  1. As mentioned, most potentiometers have a 0-270 degree rotational capability, so you will be limited to that.

  2. So - you need to scale 0-269 to 0-1023 (the map() function can help here).

  3. Take your set-point number (in degrees) to move to - say "200", scale it to 0-1023.

  4. Read the potentiometer's position - it will fall between 0 and 1023.

  5. If the value you read is less than the set value (in our case, 300), turn on the h-bridge to the motor to rotate the motor so that the value read increases.

  6. If the value you read is greater than the set value, reverse the direction of the h-bridge so that the motor rotates in the direction to cause the value read to decrease.

  7. Goto step 4/5.

That's your basic servo feedback algorithm - but a couple of points:

  1. Step 7: I say goto "step 4 or 5" because you are really monitoring your set-point position, so if it changes, you want your feedback algorithm to follow.

  2. Steps 5 and 6: When you read "greater or less than" the "set value" - read that as "within a certain percentage or window of the set value"; because the potentiometer isn't perfect, and because the mechanical linkage isn't either - you want to have a "window" of values around which, if the value of your set-point is within that window - you turn things off. That is called a "dead band" and is something you'll need to play with. Without it, though, the algorithm and the mechanical system might "hunt around" and never settle. Also note that once the system is settled "inside the window", the set-point needs to be moved outside that window in order for it to run again.

  3. Note that if something (the wind, perhaps) moves the mechanism and is able to "back-drive" the motor(s) - it will self-correct (if your gearing mechanism is back-drivable).

  4. You might consider implementing a simple PID algorithm instead of a windowing system - but the windowing system is fairly simple to implement. PID can be difficult to understand and tune.

As noted by others, you could also add an encoder (like a simple slot-detection system) onto the motor/gears, and count the "ticks". Add a travel limit switch on both ends of travel; move to one end until the switch is tripped, then move to the other, counting the ticks to the other end of travel. Then move back to the beginning, subtracting ticks - you should end up at zero (if you don't, then you'll need to come up with a solution to that). That final number will be your "range of motion"; after that, it works the same as the potentiometer - compare where you think you are to your set-point, then drive the position up or down based on the set-point, etc. Note, though, that you won't be able to determine your position from the angle at "startup" - a slot detector is a relative encoder, not an absolute encoder (you can purchase absolute encoders that encode angles digitally - but after about 8-bits of resolution, costs can climb rapidly - at least for an optical system - there do exist fairly high-resolution magnetic hall absolute encoders that tend to be fairly inexpensive).

If you must use a potentiometer - which is going to give you somewhat limited accuracy, you really need to be using a solid carbon track potentiometer or a wire-wound one - and you may need a degree of "de-bounce" code.

thank you all my dear friends your tips were very helpful .

Just a thought, but you may consider using a digital compass for the azimuth. You must compensate for declination, but that is easily done in software and there are many sites on the web to help determine declination for any Latitude and Longitude. Most compasses need to be kept level to be accurate but you need to establish a zero plane anyway. And I have to wonder if a 3-axis accelerometer could be used to measure elevation (tilt) accurately enough. You can get both along with tri-ax rate gyro on the same board (look for 9 or 10 DOF IMU).