An I2C based Pump controller using a Attiny44!

Note: All the files here

Contents

Principle

In this week’s assignment I will work on a pump driver controlled by an I2C-based Attiny. This module will be part of my Final Project and will be used to recirculate the test gases in order to provide with a flow over the installation.

The pump I will be using is an AIRPO D2028B: a vacuum pump driven by a 12V / 12W DC motor:

Image Source: Abra Electronics

The pump is driven at 12V and it will be using a power supply available at the lab with a DC Barrel Power Jack. However, in order to switch on and off the pump, I will be complicating things a little bit and use a MOSFET driven by an Attiny44. Also, as happened in the previous week this will all be controlled by a Raspberry Pi, and the voltage levels have to be taken into account, so that the I2C communication is able to run at 3.3V and the Attiny can interface with the MOSFET running the pump.

MOSFETs

MOSFET stands for Metal-Oxide-Semiconductor Field Effect Transistors and they are a type of transitor commonly used in power electronics. Before jumping into the hardware implementation, I will detail here what I have learned about them.

How a MOSFET works is quite interesting thanks to the physics of semiconductors. Semiconductors are an intermediate case between conductive materials (mainly metals) and insulators. Semiconductors’ functions are described very nicely in this link and, even if I won’t repeat everything from there here, I think it is important to understand that the real magic for semiconductors in electronics occurs when they are doped: either with electrons or lack of them, they become a different type of material and their conductivity changes drastically. They are called N-type when they are doped negatively (with electrons) and P-type when they are doped positively (with electrons removal or addition of holes).

Moving electrons between N and P-types areas can be easy or tricky, depending on the direction they go to: from N-type to P-type areas is easy, and is very difficult the other way around. This property is key for transitors in general and is the base for their functioning.

More into detail, in the case of a MOSFET, we find a combination of three layers: N-P-N (with P-type layer sandwiched between the two N-types) and P-N-P (with N-type sandwiched between two P-types). The NPN is normally called N-channel and PNP is P channel. In the case of the N-channel, there is a layer of insulating material attached to the P-type semiconductor part, and attached to it we find the so called GATE. On the other sides (N terminals), we have the so called DRAIN and SOURCE, being this last one also connected to the non-insulated side of the GATE.

Image Source: Concise electronics for geeks

When we apply voltage to the GATE in an NPN, we are attracting electrons on the GATE side of the P-type material, leaving a positive charge on the oposite side. This creates a channel between the two N-type layers (I assume that’s where N-channel comes from). The larger the voltage we apply to the GATE, ideally, the larger the ammount of electrons we can have in the channel and therefore, the lower the resistance, meaning the MOSFET is ON. This means that if 0V are applied in the GATE, electrons will not group in the P-type material and the resistance will be too high and effectively it will create an open circuit, meaning the MOSFET is OFF.

This last part defines two very important parameters: the necessary voltage to turn ON the MOSFET and it’s resistance during that operation. The voltage is normally specified as Drive Voltage (or Vgs) and the resistance is normally specified as RDS(on). The latter is indeed very important, becase the power disipated by the MOSFET will be P = I^2 R, and the larger the resistance is, the larger the heat to disipate. Interestingly, this depends on the Voltage applied in the gate, and the minimum threshold is specified by Vgs (th).

Just to compile some more information about MOSFETS, it is important to know that they can either work as a normally-on or a normally-off switch. These are called depletion type or enhacement type and they are marked as a continuous or a dotted line between DRAIN and SOURCE:

  • Depletion type: the transistor requires the Gate-Source voltage (Vgs) to switch the device “OFF”. The depletion mode MOSFET is equivalent to a “Normally Closed” switch.
  • Enhancement type: the transistor requires a Gate-Source voltage, (Vgs) to switch the device “ON”. The enhancement mode MOSFET is equivalent to a “Normally Open” switch.

Image Source: Electronics tutorials

Where to put the MOSFET

Un-luckily for me, the Lab didn’t have a reasonably small N-channel MOSFET at the time I did this assignment, so I had to think if I wanted to use a P-channel type or a bigger N-channel, which was probably a bit overkill. In order to answer those questions, I reviewed this document, this site, this other one and these videos (btw, these are very explanatory):

Image Source: Vishay

The option using the P-Channel is not that inmediate for an electronics newby like me: the P-channel MOSFET will have to run on the HIGH SIDE of the circuit and it will be easily triggered ON whenever a voltage below the Drive voltage (VDD = 12VDC in my case) is applied to the gate. However, in order to turn OFF the MOSFET, it would need at least the VDD voltage, which in my case is not available in the circuit by itself, since the Attiny runs at 5V tops. There is another option to do this and is to use an N-channel MOSFET driven by a lower GATE Voltage which outputs to the GATE of a P-channel MOSFET. In this situation, the N-Channel MOSFET can be driven by a lower GATE voltage (for example coming from the Arduino) and it will Pull down the GATE of the MOSFET below VDD. When the N-channel MOSFET is turned OFF, the GATE of the P-Channel MOSFET will be switched back to VDD and the MOSFET will turn OFF.

Image Source: Vishay

This last option is interesting, but it wouldn’t make much sense for me to do it if I have to use the same N-channel MOSFET to run a P-channel MOSFET, when the N-channel MOSFET is so big that I could do the job by only using it on the LOW SIDE of the circuit.

Having said that, now the question is: since the Raspberry Pi runs the I2C at 3.3V and the N-channel big MOSFET has a minimum RDS(on) at 5V, will the MOSFET get too warm when there is 1A running through it in normal operation? Put differently, are the 3.3V of the Pi enough to run the MOSFET at a safe condition?

To be honest, I didn’t realise about this problem until I had built the board. Nevertheless, in case this isn’t safe, the board can either be redesigned in order to run at 5V on the MOSFET side with a level shifter, or the Raspberry Pi Hat can be redesigned so that the 5V are downshifted in the SDA and SCL lines. Although this is a weird combination of Power vs I2C levels in the hat and needs to be rethought.

Design

For the design of this board I used KiCad. The schematics of the board, including an indicating LED, an ISP, the above mentioned MOSFET and a power connector is below:


The I2C connector is the same as the one done in the previous assignment, but this time with some more room for the legs to have some soldering to attach to. For the pump, I measured the terminals and created my own footprint for it:


The Pads in this case are made as through-hole, defined like:


In the case of the Motor, I added a flyback Diode and a current limiting resistor for when the Pump is turned off, following this schema:


Finally, I added a 10kΩ pull down resistor in the MOSFET GATE in order to easily turn the MOSFET OFF.

Routing

The final PCB routing looks like:


Note that I added a hole for the shaft in the PCB and two small holes under the barrell power jack for the locatin pins it has underneath.

Then, I moved onto fab.modules on the Modela MDX-20 in the lab, using the following PNG for the traces (in this same order):


For the Inner cuts:


For the Jack Holes:


And finally the outter cut:


The final board result, with all the components soldered in it looks like:


Testing the board:

I will be using a very simple blink LED sketch to test the board intially. Following the same procedure to write the fuses and set up the CLK divider as seen in the previous week.

#include <Arduino.h>

// Sensor and Indicator Led Pins
#define LED 0
#define PUMP 1

// Variables for the pressure sensor
int _delay = 1000;

void setup() {
  pinMode(LED, OUTPUT);
  pinMode(PUMP, OUTPUT);

  digitalWrite(PUMP, LOW);

  //
  // set clock divider to /1 
  //
  CLKPR = (1 << CLKPCE);
  CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
}


void loop() {

  digitalWrite(PUMP, LOW);

  digitalWrite(LED, HIGH);
  delay(_delay);
  digitalWrite(LED, LOW);
  delay(_delay);

}

This working, I moved onto checking how the the MOSFET runs at 3.3V through and see if it gets any hot with the Pump in the loop, but no connection to the Raspberry Pi I2C:

void loop() {

  digitalWrite(PUMP, HIGH);

  digitalWrite(LED, HIGH);
  delay(_delay);
  digitalWrite(LED, LOW);
  delay(_delay);

}

This makes the MOSFET to have 3Ω resistance, which might be OK for it to disipate the heat.

Then, using a power supply from the lab I tried to turn ON and OFF the pump during 10s, with a hardcoded delay:

  digitalWrite(LED, HIGH);
  digitalWrite(PUMP, HIGH);
  delay(_delay);
  digitalWrite(LED, LOW);
  digitalWrite(PUMP, LOW);
  delay(_delay);

And then… some mistakes!

Mistake #1

I connected wrongly the barrel connector. In my power supply, the inner pin (pin 1 below) is the +VDC and the outer part of the barrel is the -VDC. The barrel has three components though:


From this link: The pin 3 serves as a detector for when you insert the plug. When this happens the connection between pins 3 and 2 breaks. This is useful for cutting a battery out of the circuit when a DC adapter is plugged in. In this way, the battery is protected from two unwanted actions: discharging, and being charged. Also, this can be used for building a circuit which detects the insertion of the DC plug. For instance, when the 3-2 connection breaks, some microcontroller I/O pin could be pulled up to VCC, and then the firmware knows that the device is on an adapter.

Therefore, the connection to ground should be done also on the Pin 2, instead on pin 3, or on both, since I have no intention to detect the conection of the barrell connector:


On the first version of this board (see below for more details)

Mistake #2

When I mounted the PCB onto the pump, and turned it on, it began to work erractically.


I run some tests on the board and initially I thought it could be that the MOSFET was inducing some sort of current to the microcontroller through the GATE. For that, I tried to add a resistor in the line:


But this sadly didn’t solve the problem. Then, I was recommended by Esteban to remove the board from the top of the pump since it could be provoking some magnetic interference, or maybe the vibrations from the pump itself:


And it worked! I have done this video to show how the pump works:


I tried to reenact the problem by moving the board back on top of the pump, but I didn’t succeed. The pump’s magnetic field apparently is not a problem on its own, but maybe it is more the vibrations of the pump and probably some bad soldering I have done.

I have done a more robust version for this board below, with the following changes:

  • Add an LED in the GATE line (bad idea)
  • Add a GND copper zone around the circuit
  • Wider traces (0.6mm for the power and 0.3mm for the microcontroller)

The V2.0 is below, but for now, you can see the Frankestein board it resulted from the test:


And a final note! The MOSFET opens at 3.3V without any problem, and it does not get warm at all! even if the resistance goes up to 2Ω when operated like that.

Corrections and V2.0

The new board, with the corrections from above, working at either 3.3V or 5V to trigger the MOSFET gate:


And the new PCB routing, with a copper zone layer for the GND in which I could try to reduce the magnetic fields:


Here I detail the Copper Zone Properties in KiCad. I followed this tutorial for this purpose:


Then, here are the new PNGs for the TRACES:


The inner cuts (almost the same):


The outer cuts:


And the final board after milling!


Mistake (again…)

And of course, yet another mistake: apparently the LED drops down the voltage of the gate to a level that it’s not able to turn ON the MOSFET when powering the circuit at 3.3V. The only purpose for this item was to have a nice glowing when run at different PWMs in the MOSFET, but I had to remove it for simplicity. I replaced it with a 560Ω resistor and the 2,2kΩ by a jumper.