Page 1 of 1
PWM test sketch for Arduino UNO?
Posted: March 1st, 2016, 1:14 pm
by LePaul
I am hoping Amedee can share the UNO sketch and breadboard layout so I can test the new fans I bought.
If they work in this setup, I can consider putting them in my UMO
(reference:
viewtopic.php?f=7&t=74&start=45#p859)
Re: PWM test sketch for Arduino UNO?
Posted: March 1st, 2016, 2:45 pm
by Amedee
You have some transistor in house?
Re: PWM test sketch for Arduino UNO?
Posted: March 1st, 2016, 2:48 pm
by LePaul
I might! I have all the contents of the Sparkfun Inventor's Kit and a few odds and ends
Re: PWM test sketch for Arduino UNO?
Posted: March 1st, 2016, 2:58 pm
by Amedee
If you can give me the reference of what you have and also the Amp rating of your fan, I can check if it will work and which resistor we need...
You probably need a flyback diode as well (1N400x or whatever) unless the transistor has one builtin.
Re: PWM test sketch for Arduino UNO?
Posted: March 1st, 2016, 3:05 pm
by LePaul
Re: PWM test sketch for Arduino UNO?
Posted: March 1st, 2016, 3:34 pm
by Amedee
You are all set
So you need something like this:
For the transistor & diode, use what you have.
For the resistor, to be safe use 2 330 Ohms in series.
That gives us on the breadboard
Now, be careful with the transistor, you need the minus on the emitter and the fan/diode on the collector side (I find the drawing confusing)
Batteries are your 12V battery
Sketch in next episode
Re: PWM test sketch for Arduino UNO?
Posted: March 1st, 2016, 4:26 pm
by LePaul
Can the USB provide enough power?
Re: PWM test sketch for Arduino UNO?
Posted: March 1st, 2016, 4:28 pm
by Amedee
Here it is...
Code: Select all
/*
PWM Fan Test
*/
/*
Arduino Pin assignement
*/
// Digital
const int usbSerialRxPin = 0; // Reserved
const int usbSerialTxPin = 1; // Reserved
const int fanPin = 3; // Fan control (pwm)
/*
Globals
*/
uint8_t fanSpeed = 0; // Start as ...
/*
Setup routine
*/
void setup() {
// Serial
Serial.begin(9600);
// Fan pin setup
pinMode(fanPin, OUTPUT);
analogWrite(fanPin, fanSpeed);
Serial.println("Setup done");
Serial.println("Enter desired fan speed (0-100%)");
}
/*
Main loop
*/
void loop() {
// Check input
if (Serial.available()) {
long i = Serial.parseInt();
if (i < 0 || i > 100) {
Serial.println("Invalid input");
} else {
Serial.print("Setting fan to ");
Serial.print(i);
Serial.println("%");
fanSpeed = 255 * i / 100;
analogWrite(fanPin, fanSpeed);
}
}
}
Note that all this is untested
Re: PWM test sketch for Arduino UNO?
Posted: March 1st, 2016, 4:31 pm
by Amedee
LePaul wrote:Can the USB provide enough power?
USB powers the Arduino.
You should power the fan separately (I remember I saw A 12v battery in one of your pictures)
Re: PWM test sketch for Arduino UNO?
Posted: March 1st, 2016, 5:39 pm
by LePaul
Oh yes have many of those