Pages: [1]   Go Down
Author Topic: Arduino 100hz PWM output  (Read 1907 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
}
« Last Edit: November 23, 2011, 12:46:34 pm by Z0rr0 » Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 533
Posts: 26940
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

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.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49034
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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"
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49034
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

im just looking for confirmation that this line:

TCCR2B = TCCR2B & 0b11111000 | 0x06;

will give me about 125Hz on pin 9
Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 533
Posts: 26940
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: November 23, 2011, 10:49:35 pm by CrossRoads » Logged

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.

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 533
Posts: 26940
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


* Regulators & N-channel PWM.jpg (37.69 KB, 960x720 - viewed 78 times.)
Logged

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.

Pages: [1]   Go Up
Jump to: