<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>PWM on Bald Engineer</title><link>https://www.baldengineer.com/tag/pwm</link><description>Recent content in PWM on Bald Engineer</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Wed, 05 Jul 2017 16:46:28 +0000</lastBuildDate><atom:link href="https://www.baldengineer.com/tag/pwm/index.xml" rel="self" type="application/rss+xml"/><item><title>PWM a 3-pin PC fan with an Arduino</title><link>https://www.baldengineer.com/pwm-3-pin-pc-fan-arduino.html</link><pubDate>Wed, 05 Jul 2017 13:13:53 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=6577</guid><description>A question came up on IRC regarding how to PWM a 3-pin PC fan with an Arduino using analogWrite(). Controlling the fan was seemingly straightforward. The problem was that the hall effect sensor, or TACH signal, was incredibly noisy. The noise made it impossible to measure the fan’s rotation. Working through the question, I found three issues to tackle:&#10;You need to use a PNP transistor Filter capacitors help Create a non-blocking RPM measurement (with millis()) This post addresses all three issues regarding how to PWM a 3-pin PC fan with an Arduino.<p><img src="https://www.baldengineer.com/wp-content/uploads/2017/07/arduino-pc-fan-tutorial.png" alt="PWM a 3-pin PC fan with an Arduino"/></p></description></item><item><title>Fading LED: analogWrite millis() Example</title><link>https://www.baldengineer.com/fading-led-analogwrite-millis-example.html</link><pubDate>Wed, 23 Nov 2016 14:34:21 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=6250</guid><description>It’s a well-known fact of engineering: LEDs make everything look better. And that means a Fading LED is even better. Using Arduino’s analogWrite(), fading a LED is just a matter of a loop. If you use delay(), you can’t easily add other actions. What can you do? Well, Fading a LED with millis() is pretty simple. Here’s the code to do it and a quick explanation.&#10;//pwm-fade-with-millis.ino // Example Fading LED with analogWrite and millis() // See baldengineer.com/fading-led-analogwrite-millis-example.html for more information // Created by James Lewis const byte pwmLED = 5; // define directions for LED fade #define UP 0 #define DOWN 1 // constants for min and max PWM const int minPWM = 0; const int maxPWM = 255; // State Variable for Fade Direction byte fadeDirection = UP; // Global Fade Value // but be bigger than byte and signed, for rollover int fadeValue = 0; // How smooth to fade? byte fadeIncrement = 5; // millis() timing Variable, just for fading unsigned long previousFadeMillis; // How fast to increment? int fadeInterval = 50; void setup() { // put pwmLED into known state (off) analogWrite(pwmLED, fadeValue); } void doTheFade(unsigned long thisMillis) { // is it time to update yet? // if not, nothing happens if (thisMillis - previousFadeMillis &gt;= fadeInterval) { // yup, it's time! if (fadeDirection == UP) { fadeValue = fadeValue + fadeIncrement; if (fadeValue &gt;= maxPWM) { // At max, limit and change direction fadeValue = maxPWM; fadeDirection = DOWN; } } else { //if we aren't going up, we're going down fadeValue = fadeValue - fadeIncrement; if (fadeValue &lt;= minPWM) { // At min, limit and change direction fadeValue = minPWM; fadeDirection = UP; } } // Only need to update when it changes analogWrite(pwmLED, fadeValue); // reset millis for the next iteration (fade timer only) previousFadeMillis = thisMillis; } } void loop() { // get the current time, for this time around loop // all millis() timer checks will use this time stamp unsigned long currentMillis = millis(); doTheFade(currentMillis); } Global Variables for Fading LED Constants The constant “pwmLED” is the pin that gets faded. (Make that pin supports analogWrite().) The two #define statements, UP and DOWN, are simple states for the LED. They make the code easier to read. The “maxPWM” value is set to 255 for 8-bit AVR boards like the Uno. If you’re using the ESP8266, set this value to 1023. (You can also use the constant “PWMRANGE.”)<p><img src="https://www.baldengineer.com/wp-content/uploads/2016/11/led-fading-millis-example.jpg" alt="Fading LED: analogWrite millis() Example"/></p></description></item><item><title>Arduino pinMode(): When to use and why</title><link>https://www.baldengineer.com/when-to-use-arduinos-pinmode-and-why.html</link><pubDate>Mon, 11 Nov 2013 11:57:39 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=1923</guid><description>All Arduino boards have GPIO pins with digital and analog capabilities. The Arduino pinMode() function determines how the pins will operate. A surprise might be that in some cases it is not necessary to use it. And when you do, pinMode() may not always work the way you expect. This post outlines how the Arduino pinMode() function works and when you should (or not should) use it.&#10;pinMode(13, OUTPUT); Arduino pinMode() scope This post focuses on the 8-bit AVR Arduino boards such as the Uno, Mega, and Leonardo. The processors on these boards are the ATmega328, ATmega2560, and ATmega32u4, in that order. For the most part, this information does apply to non-AVR and 32-bit Arduino variants. When in doubt, double check with the Arduino Reference pages for details to specific platforms.</description></item></channel></rss>