To make the Teensy control the speed of a fan, I will use the built-in PWM feature of the Teensy/Arduino.
The PWM pin can send out a stream of pulses, hi = 5V, lo = 0V.
The duty cycle of the PWM, the percent of time the voltage is high, can be changed in 256 steps (0 to 255) from 0% to 100%, respectively.
NOTE: My Analog read pin can read in steps of 1024, so I will need to divide my analog read values by 4 before I direct them to the PWM output.
So I located the simplest Arduino program I could find and stripped out everything I didn't need.
I ended up with this endless loop PWM program:
Code:
//PWM test; PWM Value = 0 to 255
const int Brightness = 14; //This sets up Teensy pin 14 and names it Brightness
void setup() {
pinMode(Brightness, OUTPUT); //This makes Brightness (AKA: pin 14) an output pin
}
void loop() //This sets up a loop
{ //BEGIN LOOP
analogWrite(FanSpeed, 255); //This sends a value of 255 to Brightness
delay(500); //Delay one half second
} //REPEAT LOOP
I connected an LED to Teensy pin 14, and compiled & ran code.
LED comes on full bright.
Next I changed the 255 value to 128 and re-ran. LED came on less bright.
Ditto 64 (dimmer)
Ditto 32 (dimmer)
Ditto 16 (etc.)
.
.
.
Ditto 0 (LED off)
Everything worked as I had expected!
On to the next test.
Best,
-AC