Pages: [1]   Go Down
Author Topic: PWM not working  (Read 753 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 91
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Guys, im a bit puzzled as to why this inst working for me.
Code:
void loop(){
for(int a = 0; a < 100; a++){
  Pump(a);
  delay(200);
  }
  for(int a = 100; a > 0; a--){
  Pump(a);
  delay(200);
  }
}

void Pump(int percent){
  int pumpval = map (percent, 0, 100, 0, 255);
  analogWrite(PumpPin, pumpval);
  Serial.print ("pumpval: ");
  Serial.println(pumpval);
}

This is driving a 12v pump through a MOSFET.
watching the serial output, the numbers written to the pin count up to 255 and that is the only time the pump comes on. on for 200ms and off again. just enough to spin it up. I want to the be able to run up slowly then down again. Im sure i had this working a few days ago (didnt save it, i know stupid!!).

Im sure its something simple, i just cant see it. please help.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 634
Posts: 50239
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Im sure its something simple, i just cant see it.
Without seeing what pin you are writing to, neither can we. From what you describe, though, it would appear that you are not using a PWM pin.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26488
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Start here
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 91
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Without seeing what pin you are writing to, neither can we.
whoops. Pin 9.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 91
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have hooked up an LED to pin 9 (with a resistor) and tested the same code, as it should fade the LED. It does the same as the pump. Flashes at 255, that is all, off for the rest of the time.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26488
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's not the behaviour of a digital pin written to by analogWrite either, which switches on at 128.
Can you post all your code and your schematic?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 91
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you post all your code and your schematic?
all code, a lot of it doesnt have anything to do with my problem, but here you are:
Code:
int TempPin = A0; 
int HeadPin = A1;
int BodyPin = A2;
int TailPin = A3;
int ResHiPin = A4;
int ResLoPin = A5;
int RelayPin = 8;
int PumpPin = 9;
int ServoPin = 10;
int SpkrPin = 11;
int SolPin = 12;
int ButtonPin = 7;

int CurrentPos = 0; //set servo current pos
int pos = 0; //temp storage of pos for fluid movment
int ResLo = 0;
int ResHi = 0;
int WaterState = 0;  //state 0 = Off, State 1 = On.
int Temp = 0;   // store of temperature
int ButtonState = 0;         // variable for reading the pushbutton status


#include <Servo.h>
Servo flowservo;  // create servo object to control a servo 



void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
pinMode( RelayPin, OUTPUT);
pinMode( PumpPin, OUTPUT);
pinMode( ServoPin, OUTPUT);
pinMode( SpkrPin, OUTPUT);
pinMode( SolPin, OUTPUT);
pinMode( ButtonPin, INPUT);
flowservo.attach(ServoPin);

}

void loop() {
  //startup pause
  while (ButtonState == 0) {
    ButtonState = digitalRead(ButtonPin);  // read the state of the pushbutton value:
    Serial.println("waiting for button press");
  }
 
  for(int a = 0; a < 100; a++){
  Pump(a);
  delay(200);
  }
  for(int a = 100; a > 0; a--){
  Pump(a);
  delay(200);
  }
  //GetTemp();
  //ServoGoto(10);
  delay (2000);
  //beep(200, 2);  // beep(ms, repeats) ms= time to beep for, repeats = number of times to beep.
  //beep(400, 3);
 
  //Checks water resivoir level and turns on tap if needed.
  ResLo = analogRead(ResLoPin);
  Serial.print ("lo: ");
  Serial.println(ResLo);
  if (ResLo < 400) {
    WaterOn();
    if(ResHi = analogRead(ResHiPin) > 400){
    beep(200, 5);
    }
  }
  if (WaterState == 1){
     ResHi = analogRead(ResHiPin);
     Serial.print ("hi: ");
     Serial.println(ResHi);
     if (ResHi > 400) {
       WaterOff();
     }   
  } 
 
} // end void loop -----------------------------------------------


// pump control -----------------------------------------------------
void Pump(int percent){
  int pumpval = map (percent, 0, 100, 0, 255);
  analogWrite(PumpPin, pumpval);
  Serial.print ("pumpval: ");
  Serial.println(pumpval);
}



// Water control ------------------------------------------------------
void WaterOn(){
  digitalWrite(SolPin, HIGH);
  WaterState = 1;
}

void WaterOff(){
  digitalWrite(SolPin, LOW);
  WaterState = 0;
}


//  Power for Element control subroutine -------------------------------------------
void PowerOn(){
  digitalWrite(RelayPin, HIGH);
}

void PowerOff(){
  digitalWrite(RelayPin, LOW);
}

// Setup Beeper ----------------------------------------------------------------------

void beep(unsigned char delayms, int repeats){
   // Random frequency between 20 and 1400 (Hz).
    for(int i=0; i < repeats; i ++){
      unsigned long freq = (1400);
      tone(SpkrPin, freq, delayms);
      delay (delayms);
      freq = (1000);
      tone(SpkrPin, freq, delayms);
      delay(delayms);
    }
     

}



//   Servo control subroutine-----------------------------------------------------------
void ServoGoto(int gotopos){ 
  Serial.print("currentpos: ");
  Serial.println(CurrentPos);
  Serial.print("pos: ");
  Serial.println(pos);
  Serial.print("gotopos: ");
  Serial.println(gotopos);
  Serial.println();
 
 if (CurrentPos < gotopos){
    for(pos = CurrentPos; pos < gotopos; pos += 1)  // goes from 0 degrees to 180 degrees
    {                                  // in steps of 1 degree
      flowservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(15);                       // waits 15ms for the servo to reach the position
    }
    CurrentPos = pos;
 }
 if (CurrentPos > gotopos){
    for(pos = CurrentPos; pos > gotopos; pos-=1)     // goes from 180 degrees to 0 degrees
    {                               
      flowservo.write(pos);              // tell servo to go to position in variable 'pos'
      delay(15);                       // waits 15ms for the servo to reach the position
    }
    CurrentPos = pos;
 }
}

//  read temp subroutine ------------------------------------------------------------------
void GetTemp() {
  // read the analog in value:
  int sensorValue = analogRead(TempPin);           
  // map it to the range of the analog out:
  Temp = map(sensorValue, 20, 210, 25, 100); 

  // print the results to the serial monitor:
  Serial.print("sensor = " );                       
  Serial.print(sensorValue);     
  Serial.print("\t output = ");     
  Serial.println(Temp);   
                   
}

as for schematic... i dont really have one. so ive draw it (quiet crudely) here:


* schematicish.jpg (85.19 KB, 1075x807 - viewed 29 times.)
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17301
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I suspect your problem is your selection of the mosfet device RFP3055. It's simply not a 'logic level' mosfet and is designed to be used with gate voltage of +10vdc to switch to full on capability. Your probably just approaching turn on as you reach 255 pwm output value. Switch to a logic level mosfet and all will be good.

http://www.fairchildsemi.com/ds/RF/RFD3055SM.pdf

SparkFun sells a nice one if you want to compare datasheets:

https://www.sparkfun.com/products/10213?

http://www.sparkfun.com/datasheets/Components/General/RFP30N06LE.pdf

Lefty
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 634
Posts: 50239
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Servo class uses timers. The PWM functionality uses timers. Using a servo disables PWM on pins 9 and 10.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26488
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

...which is the reason we ask you to post all your code, or the smallest sketch which exhibits your problem.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 91
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Servo class uses timers. The PWM functionality uses timers. Using a servo disables PWM on pins 9 and 10.
Thats exactly it! Thankyou! that explains why in my testing a few days a go i could have sworn it worked. Ive changed it to Pin 3 and it works again.

I did try googling this before asking the question, no wonder i didnt find an answer, where would I have found that documented?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

where would I have found that documented?

It's described in the documentation for the Servo library.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: