jojonl
October 3, 2018, 1:44pm
1
Hi Everyone,
I have a very basic setup to control an EC Motor. It takes PWM and I can control the speed, but it won't go full speed.
Here's my code:
int potPin = A0;
int potValue = 0;
int fan = 3;
void setup() {
Serial.begin(9600);
pinMode(fan, OUTPUT);
}
void loop() {
potValue = analogRead(potPin);
analogWrite(fan, potValue);
Serial.println("work ");
delay(10);
}
so super basic. The potentiometer I use is a standard 10k.
Anything wrong with my code?
ardly
October 3, 2018, 2:28pm
2
Why not print out potValue that will probably show the problem.
jojonl
October 3, 2018, 2:41pm
3
that gives me nice 0-1023 readings.
I also tried reading the PWM value by adding
int potPin = A0;
int potValue = 0;
int fan = 3;
byte PWM_PIN = 3;
int pwm_value;
void setup() {
pinMode(fan, OUTPUT);
pinMode(PWM_PIN, INPUT);
Serial.begin(9600);
}
void loop() {
potValue = analogRead(potPin);
analogWrite(fan, potValue);
//Serial.println("Programmed by 3Ddude");
//Serial.print(potValue);
delay(10);
pwm_value = pulseIn(PWM_PIN, HIGH);
Serial.println(pwm_value);
delay(100);
}
but that gives me very weird readings, with 0 on both ends of the potentiometer
ardly
October 3, 2018, 2:50pm
4
jojonl:
that gives me nice 0-1023 readings.
...
Doesn't PWM run from 0-255?
Just try writing 255 and see if it runs full speed.
potValue has range 0-1023, the 2nd parameter to analogWrite has range 0-255!
Also setting pin 3 as OUTPUT and then immediately to INPUT doesn't make much sense.
Steve
ardly
October 3, 2018, 2:55pm
6
Always pays to read the reference manual;
void loop()
{
val = analogRead(analogPin); // read the input pin
analogWrite(ledPin, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
}