Use 3 Arduino Pins to Control As Many Devices As You Want !

11,356

45

14

Introduction: Use 3 Arduino Pins to Control As Many Devices As You Want !

This is a simple project that will allow you to control as many devices as you want with only 3 GPIO pins on a microcontroller !


Step 1: Overview

I couldn’t get pictures of the finished project yet ( I used it in a more complex project, and really don’t feel like taking it apart to get pictures :( I’ll make new ones when I get the components to do so) I will update this instructable when I can have good pictures

Microcontrollers are awesome (I use Arduino but any of them works!) ! But you may be a bit frustrated by the too small amount of GPIOs… Anyway I am !

If you want to increase the number of outputs, there is a simple integrated circuit called « shift register ». I’ll explain how it works later on.

Another thing I find annoying is that you can only control low voltage devices, with a limited amount of current.

A simple way to be able to use higher voltage outputs is to use a relay. But those are not very reliable in my experience, at leat the cheap ones. Moving parts are always a weakness, they are too slow to switch rapidly (therefore not usable for PWM… though we can’t use PWM with this PCB anyway) and they are a bit noisy too… That’s why transistors are great ! We’ll use a NPN darlington TIP120 for this PCB. They do the same thing (even more), are cheaper and more reliable !

So I looked online to find a PCB combining those two features and couldn’t find anything I liked… So I made my own ! I’m no pro (this is among my first creations) but this is a simple circuit and it works fine !

With this PCB you will be able to turn on and off 8 DC devices (up to 60V) with only 3 GPIO pins ! Cool isn’t it ? Not impressed ? Well, if you want to use even more, you’re lucky ! The shift registers ar chainable ! You just have to connect one to another and you can have as many outputs as you wish ! Still using only 3 GPIO pins ! Amazing isn’t it ?

Step 2: Materials

All the components are THT and not SMD. Be carefull when you choose them !

1 x PCB (design below)

1 x 74HC595 8 bits Shift register (multiplexer) (other shift registers should work too)

1 x 16 pin socket (optional)

8 x 1N4004 Diods (any of the 1N400X series should work)

8 x 2.2 Kohms resistors (if using arduino or other 5V GPIO devices. Details about other microcontroller later )

8 x TIP120 transistors

14 x Screw headers (5mm pitch) or direct solder to a wire or any connector with a 5mm pitch. (optional)

8 x Heatsinks depending on the current you draw (optional)

8 x plastic washers and screws.(optional)

Thermal Tape (optional)

Depending on the material you use, quantities you buy, where you buy it etc … the price of each completed PCB varies. They are around 5 to 10 €/£/$ each.

Step 3: Understanding the TIP 120 Transistors

There are lots of different types of transistors making lots of different stuff… They have literally been a revolution in electronics and you can find some in almost every electronic device. The inventors of the transistor in 1948 even got a Nobel for it !

The ones we are going to use are TIP120 , a Darlington NPN type, rated up to 60V and 5A in a TO220 package.They have 3 pins, called Base, Collector and Emitter. (The back metal plate with a hole is the Collector too).

The Emitter is connected to the common ground (so it’s sometimes called « Out Pin »).

The Collector is connected to the VCC (up to 60V DC) via the device you want to control (so it’s sometimes called « In Pin »)

The Base is connected to the control device, here the 74HC595 but it can be used directly by an Arduino pin or a simple pushbutton (so it’s sometimes called « Control Pin »).

When 5V is applied to the Base, it allows current to run from the Collector to the Emitter. If no voltage is applied to the Base, current won’t flow. Very simple !

Transistors produce heat. The more current your transistors will have to deal with, the more heat they’ll produce. But if they get too hot they will die.

So you may need heatsinks to cool them down. Just be carefull that your heatsinks don’t shortcut the collector plates to each other. So use one heatsink for each transistor, making sure they don’t touch, or one big heatsink insulated with plastic washers and screws. For better heat tranfer you should use thermal paste or thermal tape.

Whether you use heatsinks or not, you must NOT enclose the PCBs in a completely closed box. Heat must be able to dissipate in the air, through grid, holes or else...

Step 4: Understanding the Resistor ?

The datasheet of the TIP 120 says the Base-Emitter voltage is 5V. But in most schematics I could find online they use a 2,2Kohm resistor, wich lower the voltage applied to the Base. I can’t really understand why this is necessary, but I do it anyway… and it works fine. I’d love to have your insight about that in the comments ;)

I only used this PCB with an arduino. Their GPIOs use 5V. Raspberry Pi or ESP32 use 3,3V GPIO. So maybe the resistors have to be adjusted.

If you don't put any resistor, the voltage won't be reduced. So it may be okay with those controller or even with an arduino. In that case you can just put a wire instead of the resistor. Do it at your own risk … and please let me know ;)

Step 5: Understanding the Diode

The diod is not necessary if you control a LED, for example. But if you use a motor, it keeps spinning a bit after the power is off, therefore generating opposite current, for a very short time, but it can be harmfull to your transistor. This diode prevent unwanted current to kill your transistor, so I advice you use it ! They’re cheap and you can plug anything you want to the PCB without need to know whether it is mandatory or not. I you don't use it , do not put a wire istead as you would do with the resistors or you'll shortcut your circuit and risk damaging it.

Note that diode have polarity. They have to be soldered properly (there is a stripe on one side that is drawn on the PCB label).

Step 6: Understanding the 74HC595 Shift Register

Well it’s "quite" simple… and there is a LOT of documentation online that explain it much better than I could ever do !

I’ll try to summerize anyway.

There are 2 power pins : the VCC is connected to 5V, GND to... well GND!

There are 3 input pins : the « latch » pin (STPC), the « clock » pin (SHPC) and the « data » pin (DS).

There are 8 output pins that are LOW or HIGH, depending on the data the 74HC595 has been fed with. These are pins Q0 to Q7.

There is also another output pin called Q7’ (different from Q7 ) This one allows you to send extra data to another 74HC595.

There are 2 pins we won't really use, the MR (Master Reset) that will be kept HIGH (connected to 5V) ant the OE (Output Enable) that will be kept LOW (connected to GND)

So how does it work? To change the outputs configuration (which output is on or off) you need to :

- First turn the latch LOW. It enables the 74HC595 to receive data.

- Then you have to

- turn the clock LOW, so the 74HC595 knows you're sending a bit

- send the 1st bit of data (if LOW it is a 0, if HIGH it is a 1)

- turn the clock HIGH so the 74HC595 knows the bit has been sent

- repeat it for the 8 bits (or 16 if you use 2 chained registers, or 24 if you use 3 of them, etc... We'll talk about it later on) : clock LOW, 2nd bit, clock HIGH ; clock LOW, 3rd bit, clock HIGH, etc...

- Finally you have to turn the latch HIGH so the 74HC595 knows you’re done with it and refresh the state of its outputs.

Tadaa ! You have a new outputs state !

Step 7: Making the PCB

Here are the files you need to make this PCB. Image files are just here to illustrate this instructable. Use PDF files for manufacturing.

Well, there are lots of ways to get your PCB. By etching, milling etc … There are tons of tutorials to teach you how to do it.

Since I used an online PCB manufacturing service I won’t show you these techniques. (I used JLCPCB, but there are others such as PCBWAY , ELECROW and others) It costed me less than 5€ for 10 PCBs (and 5€ more for delivery). You still can use the files I provided if you want to DIY.

To have your PCB manufactured by an online maker, it's super easy. Just download the gerber files , click "add gerber" on the site you chose, choose the number of PCB you want an all the default settings are fine (You may want to change the colour though...). Then choose the shipping you prefer and pay !

The design is for double layer PCB, which is great for online manufacturing but can be a problem if you use home technics. The top side is all the data pathes, while the bottom side is all the power pathes.

Since I made it on easyeda.com you can easily re-design it for your needs. Just clone/duplicate it and change it as you want !

I may make a 1 side PCB some day and upload it here, but if you do it before me please let me know and let me use it here!

LINK TO THE GERBER FILES

LINK TO THE EASYEDA EDITOR PROJECT


Step 8: Mounting the Components

Pictures coming soon...

That’s the fun part !

This PCB is quite straightforward since each component is labelled directly on the PCB and there are few of them.

I’ll detail them anyway :

- 74HC595 : make sure it is on the top side (where the label is) and that the notch on the chip is on the proper side (as labelled) You may solder a socket instead and then plug the 74HC595 on it

- Resistors : They don’t have polarity. Just solder them on the top side (or the bottom one, it doesn’t really matter)

- Headers : You can use an kind of header that fits (5mm pitch) or directly solder wires. The PCB side you solder it on doesn’t really matter. I like screw headers because they are practical…

-Diodes : They have polarity. Make sure the striped side is as the labelled stripe. It doesn’t matter which side you solder them on as long as the polarity is respected. I prefer to solder them on the bottom side so they are not in the way of the headers.

-Transistors : I designed it so the transistors are monted on the bottom side of the PCB, with the collector metal plates face outward in order to put heatsinks on the side of the PCB and that the transistors are not in the way of the output wires (There is no label on the bottom side though, be carefull to mount it properly ). You can solder them on the top side anyway but make sure they are reversed ( metal plates facing inward as labelled). If you want to bend the transistors I advise you bend them before soldering, otherwise the pins tend to become brittle (I broke some by bending them in and out…) Bended transistor plates will face topwise if you solder them to the bottom side and bottomwise if you solder them to the top side. Make sure the Collector metal place don’t touch each other or you'll have a nice shortcut !

-Adding heatsinks : well, that depend on the heatsinks you’ll use. Just be carefull that they are insulated from the transistor itself as said earlier in the « understanding the transistors » paragraph.

It’s done !

Step 9: Coding

This coding section is made for people who know arduino code basics. If you never used arduino or coding, I doubt this is the best first project for you... But it is simple enough to be amongst your first ones !

As explained in the "understand the 74HC595" paragraph, there is quite a few steps to have the shift register working.

Since it’s a bit long to code all of this there are smart developpers who made a nice function in Arduino. It’s called… shiftOut() !

There a lots of tutorials online that will explain it better but here I go!

First you need to understand that the 8 outputs can be either HIGH or LOW. So they can be written with a set of 1 and 0. And what’s 8 1s and 0s in a row ? A Byte !

On this PCB all the positive are connected, so you have to turn an output LOW to turn on the device connected to it. A bit counter-intuitive at first but come on… you can do it ! There’s a tip later on to make it easier anyway.

So if you send 00000000 to the 74HC595, all the outputs are on. If you send 11111111, they are all off. And if you send 01100100… well it’s not that simple ! let’s see how this function works shall we ?

The shiftOUT() function need 4 parameters :

- the number of the pin connected to the DS of the 74HC595. I use a « #define SR_DATA» (SR for shift register) so it’s simpler. you can just set a variable for the same result, or type the number of the pin you'l use. Basic arduino stuff.

- the number of the pin connected to the SHCP of the 74HC595. (The same with SR_CLOCK)

-The way you want the byte you send to be read (MSBFIRST or LSBFIRST depending if you want your byte to be read from right to left or left to right)

- The byte you want to send. It can be either in binary (don’t for get the « B » before the byte, eg : B00101010) or in decimal

Since the « 1 » turn off and the « 0 » turn on,you can use the « ~ » sign to change each bit to its opposite(eg : 42 is 00101010, ~42 is 11010101) (I told you there was a tip !)

WARNING : the shiftOUT() function only send one byte at a time. If you are using several chained 74HC595s you need to use this function for each of them, the latest in the chain first, up to the first in the chain last. Each time you use this function, it « pushes » the data to the next 74HC595.

Note that you still have to turn the latch LOW and HIGH if you use this function.

byte byteToSend = 42 ;

digitalWrite(SR_LATCH, LOW) ;

shiftOut(SR_DATA, SR_CLOCK, LSBFIRST, ~byteToSend);

digitalWrite(SR_LATCH, HIGH);

You can even make a function out of it so it’s super easy to insert in your code

ShiftRegisterUpdate(byteToSend){

digitalWrite(SR_LATCH, LOW) ;

shiftOut(SR_DATA, SR_CLOCK, LSBFIRST, ~byteToSend);

digitalWrite(SR_LATCH, HIGH);

}

You then just have to use it like that :

ShiftRegisterUpdate(42) ; // if you want only the 4th, 6th and 8th devices to be on ( 42 is 00101010)

If you have several 74HC595 chained, use as many shiftOut() as you need

e .g. for 3 chained 74HC595 :

ShiftRegisterUpdate(3rdByteToSend,2ndByteToSend,1stByteToSend){

digitalWrite(SR_LATCH, LOW) ;

shiftOut(SR_DATA, SR_CLOCK, LSBFIRST, ~1stByteToSend);

shiftOut(SR_DATA, SR_CLOCK, LSBFIRST, ~2ndByteToSend);

shiftOut(SR_DATA, SR_CLOCK, LSBFIRST, ~3rdByteToSend);

digitalWrite(SR_LATCH, HIGH);

}

Note that the order of the parameters are reversed because the last chained 74HC595 receive the first byte sent

ShiftRegisterUpdate(42,86,220) ; // that will have a result of 00101010,01010110,11011100, where 1s are on and 0s ares off.

Don't forget to enable your pins to be outputs in your setup (using the pinMode() function as usual).

I'll upload a few full code examples very soon, as soon as I get the components I need to make new PCBs, Since I don't want to upload code with errors, I'll wait to be able to try it in situ ! ;)

Step 10: Connecting the PCB(s)

Since I lack some components this picture is not really a proper one, but it gives you an idea of how to daisychain those PCs

Well, it’s very simple. The first PCB need to be connected to the microcontroller . The « Data In » connexions must be connected to 3 different GPIOs (you choose which one, they all work), The « Power » +5V must be connected to the microcontroller 5V output (or an external 5v source), the GND must be connected to the microcontroller GND and the external power(s) GND (common ground) and the VCC must be connected to the external power positive.

The other PCBs you’ll use can be daisy chained, which means you just have to plug the « Data Out » pins of the first one to the « Data In » of the next one an so on. It’s the same for the « Power » pins. Unless…

If you want to use several outputs with different voltages, you can either apply the highest voltage you need to the VCC an use serial resistors on the lower voltage devices according to their own specifications. Another way to do it is to use several PCBs, one for each voltage needed, and to power each VCC pin with the proper voltage. You can then have a 9V PBC next to a 12V one, next to a 24V one etc … You get my point. All the GND must be connected together though !

Step 11: A Great Project for Classroms !

All the components are very cheap, the PCB is simple but not simplistic, and they can be used together. So I think this can be a good introduction (or maybe a level 2) to electronics and coding. As an example you can use it to make a column of « pixels » for each board using powerfull leds ( maybe 12V such as these or these )

Each student can make one, then they are chained together to build an 8 by X (number of students) matrix connected to an arduino.

Then each student can make a code to run on the arduino to display whatever you (or they) want.

A nice an simple thing to display on a matrix of 8 LEDs height is text. There are nice fonts that ca be used. You can have it moving etc …

That’s a nice way to learn about bitwise operators too.

I’d love you let me know if you used it in class !

Step 12: Conclusion

That quite simple PCB can be very helpfull, I think. There is probably a better way to do it, and I would love to know what emprovement could be done.

The comment section is open for any remark, suggestion and question. I’ll try to answer all of them ! And let me know if it was use

Have fun !

PCB Design Challenge

Participated in the
PCB Design Challenge

Be the First to Share

    Recommendations

    • Fandom Contest

      Fandom Contest
    • Backyard Contest

      Backyard Contest
    • Summer Fun: Student Design Challenge

      Summer Fun: Student Design Challenge

    14 Comments

    0
    jc_0142
    jc_0142

    1 year ago

    A nice novice project indeed. Just my 2 cents.

    "The Emitter is connected to the common ground (so it’s sometimes called « Out Pin »).
    The Collector is connected to the VCC (up to 60V DC) via the device you want to control (so it’s sometimes called « In Pin »)
    The Base is connected to the control device,
    here the 74HC595 but it can be used directly by an Arduino pin or a
    simple pushbutton (so it’s sometimes called « Control Pin »)."

    I don't know which school you came from. Being an engineer all my life with a master's degree in electrical engineering and working in the industry for over 30 years. I've never heard anyone calling the collector and "In Pin", the emitter as the "out pin" and the base as the "control pin". These labels are used for Integrated Circuits or in earlier days hybrid modules. The transistor can be configured in 3 basic modes, each with a different gains and output signal phase. Most importantly, they are ALWAYS referred to as the Base, Emitter and Collector.

    In this case, since it is driving the load, I would prefer to use a power MOSFET as the driver instead of the Darlington transistor. A power MOSFET has lower "ON" resistance, in the micro ohm range, which translates to a much lower energy loss or voltage drop at the load. That's why they are used in all the switching power supply designs.

    I used to build RC model cars way back. I used the IRF150 in a PWM controller to drive the motor. Of course, now you can just buy the H-bridge cheap on eBay. I even had a jeweler friend of mine made me some pure silver wires (I wish I could afford 14K gold wires) to connect to the motor just to lower the power loss. It was the envy of my racing buddies back then. :-)

    0
    djapipol42
    djapipol42

    Reply 1 year ago

    thanks for you answer !
    I've never learned much electronics in school I must admit... I "learned" it by myself mainly online... where I found several tutorials and explainations. One one them called the pins "in/out/control" . I found it much less confusing than the hermetic "collector/emmitter/base".

    Since I'm a begginner I know that it's quite difficult to understand /remember all the technical terms of a new craft. So I added these "unorthodox" terms because they made it easier for me to understand it. But I know the "proper" terms should be used so I never used the "not-proper" terms in the instructable. I just called it once as a side note in case they can make it easier for other begginners (whom this instructable is intended to).

    I still don't know where to use a darlington instead of a MOSFET (nor the other way around). Could we use a MOSFET directly on this PCB instead of the darlington? (the BCE pins order must be the same obvioulsy, but is there any difference in the circuit itself? If there is none, let me know what MOSFET you would advice me to use so I can edit the instructable !

    I am aware that this PCB could be much improved and that it wouldn't
    get a good grade at any electronic school test ;). Especially on the
    power consumption. But maybe as well about the design rules, the components choice (you talk about mosfets, sentinel_infinitus mentionned optocouplers, maybe some other diodes, resistors or even maybe capacitors could improve the whole thing).

    I still need to learn but this PCB works fine for the hobbyist use I needed it for. It is not designed for power optimisation nor for top precision control. It's just a cool and easy way to start electronics, and above all programming.

    My next ones will be better thanks to you and all the people that share the knowledge !

    1
    sentinel_infinitus
    sentinel_infinitus

    1 year ago

    nice pcb if i may make a suggestion optoisolators may be a good addtion to the pcb as it would help prevent stuff like a power surge from killing the shift registers

    0
    djapipol1
    djapipol1

    Reply 1 year ago

    I wanted to make this board as cheap and simple as possible...
    Don't you think the diodes are enough to prevent any power surge?
    where would you suggest to add those optoisolators?

    1
    sentinel_infinitus
    sentinel_infinitus

    Reply 1 year ago

    no diodes are not enough optoisolators are essetually diodes on there own but they isolate your logic from the load even on relay boards like this https://www.ebay.ca/itm/12V-1-2-4-8-16-Channel-Rel...
    the reason being that the relays have a coil that stores current when active for a better explanation greatscoot on YouTube did a electronics basics on galvanic isolation this would be the best example of what I am talking about but either way you would just need to replace the diodes with optoisolators as they pretty much do the same thing also may sure to add a new power connector for things on the other end of the optoisolators so they don't share the same power source as your logic one side of them need to connected to your logics power then the other side needs to have it's own power in

    better to replace one optoisolator then to replace the logic stuff like the arduino or shift register if you manage to feedback a ton of power by accident (trust me I have done this before and released a lot of magic blue smoke(magic blue smoke is a joke relating to integrated circuits ))

    0
    djapipol1
    djapipol1

    Reply 1 year ago

    Well optocouplers seems quite interesting components indeed ... never used any but I'll lok into it more seriously ! They surely prevent some damages... I know exactly what "magic blue smoke " you're reffering to... I've seen and smelled it too... it smells like carelessness :( And a bit like "learning from mistakes" too !
    But if you don't mess up too much this board is quite fine.

    thanks for your advice !

    0
    sentinel_infinitus
    sentinel_infinitus

    Reply 1 year ago

    well it's always a good learning opportunity i would like to see a version where isolation is taken a lot more seriously for example a good idea would be to have another board for isolating power from say mains to both your logic and your hopefully optoisolated outputs

    i don't know what your doing for your logic side as a whole but i will say that one thing to think about is adding a backup battery and maybe a key matrix for disabling certain outputs

    i will say for technical help you can always check with the comunity on the adafruit discordi am usaly there in the offtopic or in one of the help with channel

    0
    sentinel_infinitus
    sentinel_infinitus

    Reply 1 year ago

    also looking at it again the optisolator go where the risistors are not the diodes sorry

    0
    TheGrue
    TheGrue

    1 year ago on Step 4

    The resistors in this case are used to limit how much electric current travels through the base of the transistor. The Ohm's law formula is this: E(Volts)/I(Amps)*R(Ohms).
    So this would limit the current to 2.2mA (miliamps)
    5V÷2200(Ohms)=0.0022(Amps or 2.2mA)

    0
    djapipol1
    djapipol1

    Reply 1 year ago

    thanks for your answer !
    so if the input is 3.3V instead of 5v (as a raspberry pi GPIO) 1.5kohms should be used instead of 2.2kohms, right?

    0
    TheGrue
    TheGrue

    Reply 1 year ago

    Yes that is correct. Keep in mind that I did not check the Datasheet for this transistor to see what the Base current range it is capable of handling. I would go to Digikey.com and look up this part to find the Datasheet with the exact specs because you may still be able to use the 2.2K resistor if the Base can do the same work with 1.5mA current.

    0
    deanhemm
    deanhemm

    1 year ago on Step 12

    maybe I missed it but how about a schematic and a sample sketch??

    0
    djapipol1
    djapipol1

    Reply 1 year ago

    you're right, I should add a schematics ;) ! Will do soon !
    for now you can find it on easy eda (I don't know why the link died, I'll edit that right now)
    As I said in the "coding" section, I'll put a sample code as soon as I can try it on the board (I am waiting for components to make new PCB, the ones I already made are inside abigger project I don't want to take apart)
    I have found some crap code online and it's horrible to look for an electronic problem when it's the "test" code that's wrong... So I DON'T want to put a code I didn't try...

    coming soon !

    =edit= it seems to take some time for the edit to be active, so here is the url of the project on easyeda where you can find the schematics
    https://easyeda.com/djapipol42/arkenes-mur-coffres