Go Down

Topic: Arduino 100hz PWM output (Read 2 times) previous topic - next topic

Z0rr0

Nov 23, 2011, 06:49 am Last Edit: Nov 23, 2011, 06:46 pm by Z0rr0 Reason: 1
I'm trying to get 100Hz PWM out of pin9 to drive a DC motor, and vary the duty cycle via a Pot.
Here is the code i have so far. I'm VERY new to this.

Any Help would be appreciated

int sensorPin = A0;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor
int outputValue = 0;
void setup(){
  Serial.begin(19200);
pinMode(9, OUTPUT);
TCCR2B = TCCR2B & 0b11111000 | 0x06;
}
void loop(){
  sensorValue = analogRead(sensorPin);
  outputValue = map(sensorValue, 0, 1023, 0, 255);
  analogWrite(9, outputValue);
  Serial.println(outputValue);
}

CrossRoads

Have you read this
http://www.arduino.cc/playground/Main/TimerPWMCheatsheet
& this?
http://www.arcfn.com/2009/07/secrets-of-arduino-pwm.html

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

PaulS

Posting code that won't compile wastes everybody's time.

Z0rr0


Posting code that won't compile wastes everybody's time.


really?

it would have been much easier for you to say, "Hey man, you are missing a } at the end of your setup statement"

PaulS

Quote
it would have been much easier for you to
post code that compiles.

Z0rr0


Quote
it would have been much easier for you to
post code that compiles.


I fixed it, if you don't have anything to say about the question i am asking, Please GTFO.

Z0rr0

im just looking for confirmation that this line:

TCCR2B = TCCR2B & 0b11111000 | 0x06;

will give me about 125Hz on pin 9

CrossRoads

#7
Nov 24, 2011, 04:48 am Last Edit: Nov 24, 2011, 04:49 am by CrossRoads Reason: 1
I think you need 0x04 vs 0x06 per here

http://www.arduino.cc/playground/Main/TimerPWMCheatsheet

Pins 9 and 10: controlled by timer 1
Setting          Divisor           Frequency
0x01        1        31250
0x02        8        3906.25
0x03        64        488.28125
0x04        256        122.0703125
0x05        1024        30.517578125

to yield 122 Hz.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Z0rr0


I think you need 0x04 vs 0x06 per here

http://www.arduino.cc/playground/Main/TimerPWMCheatsheet

Pins 9 and 10: controlled by timer 1
Setting          Divisor           Frequency
0x01        1        31250
0x02        8        3906.25
0x03        64        488.28125
0x04        256        122.0703125
0x05        1024        30.517578125

to yield 122 Hz.


awesome, thanks. I'll give that a try.

i'm trying to switch a 12V load, and when i hook up 12V to the fet, it powers up the arduino. I fear i may need a diode or something on the emitter pin of the fet. Any ideas?

CrossRoads

Sounds like you have the FET wired wrong.
Should look like this as example (from a prior discussion - right hand regulator was meant to be 7805), with a Logic Level FET.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Go Up