MOSFET 101 – 2 lessons learned and a working AVR project

This article is in a series about making a shelf with lights controlled by an AVR.
Read about 
 - designing and building the book-case
 - understanding Mosfets
 - and coding the AVR, and improving it using Atmel Studio
MOSFET IRLB8721. From left: Gate, Drain, Source.
MOSFET IRLB8721. From left: Gate, Drain, Source.
MOSFET test jig with Arduino
MOSFET test jig with Arduino. Lit up by a piece of LED Strip

This post shows how I am making the circuit board for a shelf light with an Atmel Atmega168 controlling a Mosfet. And I have come to the point where I let the 240 volt wild wall horse loose on my first Atmega project. Through a 12V AC to DC converter, of course.

It didn’t work.

But I’m very happy to share why. If I wasn’t able to tell you, I wouldn’t be happy at all.

First: wrong reference voltage on MOSFET
I placed the MOSFET on high side (positive) of the LED strips. That’s a mistake because the gate voltage on the MOSFET is a reference relative to the source, and 5V (full PWM) isn’t much compared to 12V of the flowing current. When I place it after the LED strips (negative), the source is connected to ground, and 5V from the Atmega is much higher than the zero volts of ground. Hence current flows. After fixing this, when I apply voltage through PWM from the Atmega, it has the same zero reference (ground), and the gate opens for current.

Second: wiring it the wrong way
wiring of the MOSFET goes: input to Drain, and output to Source. I did it the other way around.

I hooked up my Arduino to the computer to do some MOSFET testing, it’s very fast to run a program with analogWrite to see if the MOSFET responds to the PWM output. I opened the Fade example sketch in the Arduino software, and just set my own brightness to test with varying light intensity. Even faster testing is to connect the 5V or 3.3V outputs on the Arduino to the MOSFET (ensure common ground).

NOTE: when testing with two power sources, make sure they are grounded together, otherwise, the strangest behaviour from the MOSFET will be seen as voltage is always relative to another point. If the base (ground) is not defined and equal for the two power sources, the behaviour will be spurious.

My new PCB now looks like the Fritzing attached, I had to redo some soldering and move some wires to get there, but the initial design was more “there I did it” than “optimimum, optima, optimus”, so I guess that wasn’t too bad after all.

And now the circuit it actually works!

I have purchased several types of LED strips from ebay (all from this dealer), and they come with varying light intensity (power usage) and color.

A LED strip is a 12V section of 3 LED’s and a resistor in series. They light up very brightly, can run a little warm, glue to a surface, and consume less than 200mA for half a meter. They can easily be cut and soldered at every 10 cm, and can be wired together in series.

In my next blog post I’ll share the rest of the code for the Atmega, how I use one button to cycle through power settings.

An advice on resoldering: Use a vacuum de-solder pump. It’s easy to use, and it actually removes excess solder from the PCB. The solder wick you can buy has never helped me anything, it takes long time to heat up the solder through the wick, heating everything else up as well, and it never sucks all the solder you would like to.

Final, working PCB for shelf light. Fixed MOSFET mistakes
Final, working PCB for shelf light. Fixed MOSFET mistakes

Expansion possibilities with shelf light.

This article is in a series about making a shelf with lights controlled by an AVR.
Read about 
 - designing and building the book-case
 - understanding Mosfets
 - and coding the AVR, and improving it using Atmel Studio

My shelf needs light, and I am making a simple circuit to control the light, based on an Atmega 168 and a transistor. It will have indicator lights, a switch, input power and output power. And many, many more features if I ever decide to expand. The light itself is a LED strip bought on ebay.

The Atmega AVR has loads of functions I don’t plan to use here, like timers, digital to analog converters, interrupts, and communication protocols. What I will use is one of the output rails for lighting LEDs, one rail for button switch, and one for controlling the transistor input voltage by PWM. By using different rails for each of the features, I hope to reduce the number of and severity of mistakes.

Circuit soldered and ready for Atmega chip
Circuit soldered and ready for Atmega chip. Sitting on my wonderful Hakko

This is actually my first implementation of an AVR in a project except for light flashing, so I will be documenting each step of the code.

You can see the circuit soldered to a PCB sitting on my Hakko soldering station. The circuit consists of a 5v regulator for the AVR (7805), a transistor to control the output power to the LED strips, and a switch for turning on and off the LED strip, as well as dimming it.

With the Atmega chip as base and free space on the PCB, I could potentially add

  • a motor that can open and close a door
  • an ultrasonic sensor for variable light intensity
  • a display showing jokes
  • a husher for the kids (the slapper is not so popular with my wife)
  • or whatever imagination can think of

Fritzing is a great tool to design and illustrate circuits, and I have made a version showing the concept on breadboard, and the final PCB.

Breadboard layout of circuit. Designed with fritzing.
Breadboard layout of circuit. Designed with fritzing.
PCB layout of circuit. Designed with fritzing.
PCB layout of circuit. Designed with fritzing.

Moving to the code part of the project, I have to reference the fantastic book I have read to learn how the Atmega AVR works, Make: AVR Programming by Elliot Williams. If you want to be able to make anything smart, cool, amazing, flashy, noisy, ridiculous or clever; read this book and get making.

More details on building and coding in the next blog post, for now we can agree that I am making an on/off switch, with some serious expansion capabilities.