Saturday, March 3, 2012

MOSFET Control

We have covered MOSFET control before, But here we are taking the previous PWM sketch (no code changes), and replacing the Arduino driven LED with a MOSFET and 3 high brightness LED's powered by an external 12v supply. This could easily be a heater or a motor instead of lights.

12v+ connects to the anode of the first LED in the series of 3. The cathode of the last LED connects to R1 (120 ohm), and then the drain of the IRL520 MOSFET. The source of the MOSFET connects to ground. The gate connects to R2 (10k ohm) to ground, and to R3 (130 ohm, protects the Arduino) to Arduino pin 7. It's important that the external supply is grounded to the Arduino. See the schematic for further details.

Controlling a LED Brightness

Building on the previous post, where we learned how to read a potentiometer, we now use that information to control the brightness of a LED.

Using Pulse Width Modulation (PWM), we send the full 5 volts to the LED and current limiting resistor, but we change the on and off times of the 5v. The more time the signal is on, the brighter the LED.

The following sketch adds a couple of lines to the previous sketch to control the output. Connect a LED and an appropriate resistor between Pin 7 and Gnd. Flat side of the LED points to Gnd. You can determine the correct resistor at, but a 120-150 ohm is usually workable.

int ledPin = 7;      // LED connected to digital pin 7
int analogPin = 3;   // potentiometer connected to analog pin 3
int val = 0;         // variable to store the read value

void setup()
  pinMode(ledPin, OUTPUT);   // sets the pin as output

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

Next we will discuss powering heavier loads with a MOSFET.

Reading a potentiometer

Reading a potentiometer is a simple thing to do. Connect a 3 leg potentiometer to your Arduino. The middle leg connects to a analog port. We used analog 3. the two outer legs connect to 5v and Gnd, respectively.

Insert the following code into your editor, upload, and run the serial monitor at 9600. As you rotate your potentiometer, the values change in the serial monitor.

int analogPin = 3;     // potentiometer wiper (middle terminal) connected to analog pin 3
                       // outside leads to ground and +5V
int val = 0;           // variable to store the value read

void setup()
  Serial.begin(9600);          //  setup serial

void loop()
  val = analogRead(analogPin);    // read the input pin
  Serial.println(val);             // debug value

Future posts will show you what you can do with this module.

PWM and Speed Control

I'm going to revisit, and expand previous posts on this blog, by breaking down a speed control project into modules. This post describes the project, then following posts will document each module as we expand the concept, and add more features.

The first item we want to build is to give the Arduino input on what we want the motor to do. This is done with a potentiometer and the analogRead() command.

The analogRead() reads the position of the wiper on a variable resistor (potentiometer) as 0-1023.

Now we want to use that information to control the brightness of a light, and eventually, the speed of a motor. This will be done with a command called analogWrite(). The AnalogWrite() command sends a Pulse Width Modulation (PWM) signal to the output pin, controlling the on and off times (duty cycle) of the attached device, unlike a rheostat which would reduce the voltage to the device.

An example of analogRead() and analogWrite() is shown here in our previous PWM LED Brightness Control project.

The Arduino is limited in it's voltage and current handling, so if we want to control the speed of a 12v motor pulling an amp of current (or a high current string of lights), we will have to use an intermediate device called a MOSFET. This is a type of transistor, and we are using it as a switch, that can handle high currents and fast switching speeds.

Finally, we want to know the actual rpm of the motor we are controlling, and be able to maintain under load (electronic governor). This function will use a Hall Effect Sensor to read the rpm, and we will implement a feedback loop in software to speed the motor up or slow it down based on the desired rpm set by the potentiometer.

So that's the project, stay tuned for code, images, and video.