NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Pot as Input for ADC to control PWM

October 17, 2010
by vburguess
vburguess's Avatar

To start off with I am am beginner to code and micro controllers. Im trying to control motor speed using a pot, im not sure how to set up the pot so that it does this. I have looked through the motor tutorial and the servo squirter and got most of my code from there, I feel that its a scaling issue but im not sure how the pot works with the ADC so im not sure what to set the scale to or what specs off the pot I need to calculate the scale. Also I copied most of the ADC code from the temp sensor motor control tutorial but it seems like when I adjust the pot it doesn't automatically adjust, I have to cycle the power to get it to change or at least thats what the LCD is saying. I basically deleted what I thought I didnt need from the other code samples but there is a possibility that I deleted too much and thats why it wont work right.

here are screen shots of my code: http://i165.photobucket.com/albums/u54/audi90kid/Screenshot2010-10-17at40834PM.png

http://i165.photobucket.com/albums/u54/audi90kid/Screenshot2010-10-17at40903PM.png

October 17, 2010
by Ralphxyz
Ralphxyz's Avatar

Just load the original tempsensor project with the pot connected to the ADC you will see the different values on the LCD. Then take it from there. Use a copy of the tempsensor code and once you see the pot effects then modify the working code, one step at a time.

Ralph

October 17, 2010
by vburguess
vburguess's Avatar

ok thanks for the help, I guess I was trying to do too much all at once which is why I got so confused. Ill try that next.

October 19, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi vburguyess,

Ralph's suggestion is absolutely the best way to go. Always simplify the problems and tackle one at a time. I suspect you were not connecting the pot correctly to be able to read its position as a voltage. What you want to do is set up one leg connected to GND, one leg connected to +5 and the middle leg connected to the ADC. This will set up a voltage divider with some fraction of the total resistance of the potentiometer before the middle leg, and the rest after it. This means that if the dial of the pot is set all the way to one side, all the resistance will be before the pin with the ADC connected to it, which means all the voltage will be dropped before it and the ADC pin will be at zero. With the pot all the way to the other end, all the voltage is dropped after the ADC and the pin is at +5; with the pot somewhere in the middle the ADC will be somewhere between 0 and 5. Hope that makes sense. If you draw and post a picture of how your pot is connected we would be happy try and help you with it.

Humberto

December 22, 2010
by VictorsNerdery
VictorsNerdery's Avatar

Mind explaining what this code is doing step by step? it would be much appreciated :)

void adc_init() {
  // set analog to digital converter
  // for external reference (5v), single ended input ADC0
  ADMUX = 0;

  // set analog to digital converter
  // to be enabled, with a clock prescale of 1/128
  // so that the ADC clock runs at 115.2kHz.
  ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);

  // fire a conversion just to get the ADC warmed up
  ADCSRA |= (1<<ADSC);
}
December 23, 2010
by bretm
bretm's Avatar

Page 49 and 50 of the NerdKits Guide explains those lines.

December 28, 2010
by Hexorg
Hexorg's Avatar

vburguess, taking screen-shots of the code is a long and tiresome way, use pastebin.org instead. You can upload your whole code, and you don't even need to register there.

Also, you say that you have to cycle the power in the nerdkit for changes to take effect, and in your code I don't see the main loop that should run forever. Remember your code must never leave the main() function, to acheive that, simply put the code that you want to be looped into a simple infinite while loop:

while(1)
{
    /*Type code here*/
}
December 28, 2010
by Ralphxyz
Ralphxyz's Avatar

vburguyess, did you ever get the pot to control the PWM (motor speed control).

I have the pot to ADC working now I need to understand PWM better could you post your working code?

Thanks,

Ralph

Post a Reply

Please log in to post a reply.

Did you know that interrupts can cause problems if you're not careful about timing and memory access? Learn more...