PDA

View Full Version : ADC0831 scaling output to drive a servo 0-5V

Turnbull2112
02-05-2010, 10:49 AM
How would I scale the voltage reading from an ADC0831 0-5V to drive a servo open and closed with PULSOUT? I want the servo to maintain its position based on the voltage. I've completed the servo projects and the ADC projects but I'm having difficulty making the connection between the two. Any help/examples would be greatly appreciated!!

Thanks

Franklin
02-05-2010, 10:54 AM
Please use the attachment manager in the post reply button to attach your programs to your next post.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen

Turnbull2112
02-05-2010, 11:04 PM
Here is my program right now. The servo just twitches and doesn't seem to respond. I am sure my math scaling is a mess. I am trying to get a 4-20mA source fed to the ADC0831 to scale 0-5 or 1-5VDC. I am using a 250Ohm resistor in parallel to the Vin (+) and Vin (-). The voltage reading is jumping around, I am not sure how to reference the 4-20mA input. Any help would be nice, please don't laugh at me!! Schematic to follow.

' {\$STAMP BS2}
' {\$PBASIC 2.5}

CS PIN 0
Clock PIN 1
DataIn PIN 2
SERVO PIN 14

Cnts2Mv CON \$139C 'x 19.6 (to mV)

result VAR Byte
mVolts VAR Word

Reset:
DEBUG CLS,
"ADC....", CR,
"volts..."

Main:
DO
GOSUB Read_0831
mVolts = result */ Cnts2Mv

mVolts = mVolts */185
mVolts = mVolts + 500
PULSOUT 14, mVolts

DEBUG HOME,
CRSRXY, 9, 0, DEC result, CLREOL,
CRSRXY, 9, 1, DEC mVolts DIG 3,
".", DEC3 mVolts

PAUSE 100
LOOP

'------------[subrtnes]----------------------------------

Read_0831:

LOW CS
SHIFTIN DataIn, Clock, MSBPOST, [result\9]
HIGH CS
RETURN

Mike Green
02-05-2010, 11:10 PM
The servo twitches because you need to send it a stream of control pulses at least 50 times a second or it will shut down until the next control pulse comes in. You're sending it pulses way less than 10 times a second if you figure in the PAUSE time (100ms) and the Read_0831 time (3ms?) and the DEBUG statement time (20ms?).

If you look at most programs that drive servos, you'll see that they use a PAUSE 20 (or less) to get the 50 times a second rate.

Turnbull2112
02-06-2010, 01:15 AM
Okay, I made some changes (thanks) and have the servo responding somewhat. I don't believe I'm getting the full sweep from 1-5VDC. The servo won't make it through 90* of rotation. I am having a difficult time understanding how the "*/" operator works or even if I need to use it here. I want to scale the "mVolts" at 1VDC to 0* rotation and "mVolts" at 5VDC to 90* rotation. Attached is my modified program. Thanks again for the support!!!!

' {\$STAMP BS2}
' {\$PBASIC 2.5}

CS PIN 0
Clock PIN 1
DataIn PIN 2
SERVO PIN 14

Cnts2Mv CON \$139C 'x 19.6 (to mV)

result VAR Byte
mVolts VAR Word

Reset:
DEBUG CLS,
"ADC....", CR,
"volts..."

Main:
DO
GOSUB Read_0831
mVolts = result */ Cnts2Mv

DEBUG HOME,
CRSRXY, 9, 0, DEC result, CLREOL,
CRSRXY, 9, 1, DEC mVolts DIG 3,
".", DEC3 mVolts

mVolts = mVolts */ 15
mVolts = mVolts + 700
PULSOUT 14, mVolts

PAUSE 10
LOOP

'------------[subrtnes]----------------------------------

Read_0831:

LOW CS
SHIFTIN DataIn, Clock, MSBPOST, [result\9]
HIGH CS
RETURN

Mike Green
02-06-2010, 01:50 AM
Have you read the description of the "*/" operator on page 112 of the Stamp Manual? What do you understand or not understand about it?

Turnbull2112
02-06-2010, 02:37 AM
I've read it but I am not a math star. I am asking what the ratios should be to get 1 volt to equal 0* and 5 volts to equal 90*. Is the Multiply Middle operator even necessary here? Can anyone point me in the right direction with some specific answers? Thanks.

Mike Green
02-06-2010, 02:53 AM
First of all, you need to determine the range of PULSOUT values to make your servo move the way you want. It could be 625 to 875 or 500 to 750 or 750 to 875 or something similar. You're the only one with the specific setup. The typical range of pulse widths is 1ms to 2ms for a range of movement on the order of 180 degrees, but this can vary widely with different servos. With a BS2, the PULSOUT unit is 2us, so that pulse width range translates into a PULSOUT range of 500 to 1000.

The ADC0831 has a range of 0 to 255 for voltages from 0 to 5V (typically). Once you determine the PULSOUT value range, you can come up with a formula to express it and it's easy to convert that to a PBasic statement.

Say that your servo moves the way you want with a PULSOUT value range of 625 to 875. That's a range of 250, very close to the range of values returned by the ADC0831. You could simply add 625 to the value returned by the ADC and use that in the PULSOUT statement.

Post Edited (Mike Green) : 2/5/2010 8:01:21 PM GMT

Turnbull2112
02-06-2010, 03:49 AM
Thank you Mike! That set me in the right direction!

Turnbull2112
02-06-2010, 04:52 AM
' {\$STAMP BS2}
' {\$PBASIC 2.5}

CS PIN 0
Clock PIN 1
DataIn PIN 2
SERVO PIN 14

Cnts2Mv CON \$139C 'x 19.6 (to mV)

result VAR Byte
mVolts VAR Word

Reset:
DEBUG CLS,
"ADC....", CR,
"volts..."

Main:
DO
GOSUB Read_0831
mVolts = result */ Cnts2Mv

DEBUG HOME,
CRSRXY, 9, 0, DEC result, CLREOL,
CRSRXY, 9, 1, DEC mVolts DIG 3,
".", DEC3 mVolts

IF result < 50 THEN result = 50
result = result + 622
PULSOUT 14, result

PAUSE 10
LOOP

'------------[subrtnes]----------------------------------

Read_0831:

LOW CS
SHIFTIN DataIn, Clock, MSBPOST, [result\9]
HIGH CS
RETURN

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Rob Turnbull
Audaci Favet Fortuna

Turnbull2112
02-08-2010, 09:28 PM
Did Josh or David have any luck with this mock up in the shop? Let me know if you had any luck eliminating the short cycling. Thanks.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Rob Turnbull
Audaci Favet Fortuna

Turnbull2112
02-13-2010, 09:43 PM
I am still not getting the full 180* of travel with the program i'm using. It only makes it about 70-80*. Does anyone have any advice? Thanks again!

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Rob Turnbull
Audaci Favet Fortuna

Mike Green
02-13-2010, 10:04 PM
Servos are very variable (from manufacturer to manufacturer and from model to model) in terms of their range of motion and the width of the servo pulses that they respond to. Most servos use a pulse width range from 1ms to 2ms centered at 1.5ms. Some servos use a pulse width range closer to 0.5ms to 2.5ms. Some servos just won't give you 180 degrees of travel while some will give you 270 degrees or more. Do some experiments with your servo to see what pulse width range you need.