Raspberry Pi Automation #1: Mains Relay HAT

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Mains power on a HAT, what could go wrong?

👍︎︎ 3 👤︎︎ u/[deleted] 📅︎︎ Mar 31 2019 🗫︎ replies

I'm interested in this for power-cycling mains-powered devices for the purpose of test automation.

Ideally there would be a power board that could be controlled by USB and wouldn't require mains wiring.

Any particular reason such a thing does not exist?

Or put another way - a power board "case" for RPi that happens has three Pi-controlled A/C outlets?

👍︎︎ 1 👤︎︎ u/nigels_com 📅︎︎ Mar 31 2019 🗫︎ replies
Captions
[Music] welcome to another video from explaining computers comm this time I'm going to kick off an occasional series in which I've gone to investigate the use of a Raspberry Pi in home automation or indeed nearly other kind of automation you might want to try now specifically in this video we're going to be looking at the contents of this box which contains a hats a piece of hardware to go on top of the pie which gives us three mains relays so we can control mains devices turn them on and off using a rusty part now given that we're going to be using mains electricity in this video I don't want to sound a note of caution mains electricity can clearly be very dangerous so if you don't have previous experience wiring up main circuits and using mains electricity please don't try the things I'm going to do in this video anyway with that caution out of the way - please take care let's go and open up this rather exciting little box so here we have our exciting little cardboard box containing on you raspy PI peripheral which technically is an electronic selim Raspberry Pi power relay board expansion module and this cost me 24 pounds from Amazon in the UK and it cost about 17 dollars from Amazon in United States and about 22 euros for a very similar looking product on the German Amazon website and of course all the links will be in the video description so we get inside I bring in a Stanley of a knife and just cut through the thing on the back there should be a very straightforward unboxing even for me oh it is look there it is inside and oh it's analysis cutting bag good to see those not always getting those two these days we'll just bring in mr. scissors to get to inside here and there we are coupe or crinkle crinkle hope it'll come out come out come out and a very on there is our relay board and if we take a bit of a closer look at it you can see that this is a hat with hat standing for Hardware attached on top and hat is basically a standard for peripheral boards for the Raspberry Pi that was introduced by the Raspberry Pi foundation back in 2014 and we turn this thing over your suit will attach to the Raspberry Pi using a GPO connector and it's got some Rises which was separated from the board and allow it to be attached to the board and this would fit on top of the most raspberry PI's that have been made specifically it still fit on the original Raspberry Pi B+ and the Raspberry Pi to be a Raspberry Pi 3 be arrested by three B plus and indeed the Russian PI 3 8 plus as well so those are gold lots of different raspberry PI's so what is it - well basically we've got three relays here and each of these relays is interfaced to a GPIO pin on the Raspberry Pi Vyra Darlington transistor which allows the PI's to switch these relays and the relays will actually control quite high power devices specifically they can switch up to 10 amps at up to 240 volt AC or up to 5 amps at a 30 volt DC and one of the nice things about this board is although each relay is hardwired initially to a particular GPIO pin it doesn't have to be because we look down here you can see we've got some and these make the hardwired connections between the relays relays 1 2 & 3 2 the GPIO pins at 21 20 and 26 but we could take these jumpers off and put wires across from these pins to the particular GPIO outputs we want you to use to trigger the relays so that's basically what this thing does how it works let's now put it onto a Raspberry Pi here's a Raspberry Pi 3 model b+ just waiting to have the board put onto it and I've noticed that we can't put it straight on because the screws here already in - I've got to get those out first and there we are those are out so this would now fit onto it of the Raspberry Pi would go on something like this I think if I can just get it on the correctly like that it'll push down into place there we are that's onto the Raspberry Pi and it needs to be connected on and you could put the screws back in here obviously to hold it in place but I'm not actually going to do that I'm going to use these which is some more risers either I purchased I'm going to put the riders under here because I want to be able to have this connected to a base board because I want to connect you down because we are going to be switching maintance components so I'll just do that and there we are the board is all ready to go everything all this hardware under there but that is now ready to use to a control our mains devices so there we are it is all set up I think I'll now go on a think about some appropriate Python code you so here I am back again we've got the pie all up and running and if you think this shot looks a little bit dark little bit grainy the reason is we've got a lighting set so we can see illuminated LEDs and this LED on the board just means the Hattie's on but there's also three LEDs in front of the relays which we can use for test purposes so if we go across to a raspbian and yes I've got a rod logic lock on my desktop we'll be using that later in one of our tests but for now I'm going to go to a programming and to a Python 3 to the interactive development environment and there we are that's come up there and if I just bring in a piece of test code I've written two tests out there the unit over there that'll come in and what have we got going on here well basically we're bringing in two libraries GPIO and time that's happening there and then we're setting the numbering mode for the GPIO connector and defining some output pins and it's worth noting that there are two ways you can define them numbers for the GPIO pins on the Raspberry Pi for referring to them board numbering where you just basically label the pins one two three four five six across in a nice straightforward way that's what I normally use but there's also a mode called BCM which is what we're using here and BCM is the Broadcom internal system for numbering the GPIO pins and that's what they're using by default on this board in terms of the fact that if we member on the board there was some jumper set to use the GPIO pins twenty twenty six and twenty one to control the relays and that's under BCM numbering so we could use board numbering and move these numbers around but for now we'll stick with BCM and i've set up here the output pins therefore to be twenty twenty six and twenty one I then got a little cycle headers to turn the relays on off in sequence I've got this inside a try finally configuration so that when we crash out of this using control-c there's no other way out of this it'll always clean up the GPIO serve at the end by executing the finally stuff there but inside try with then got a while loop it says while true so this will always be true this loop will go on forever it's basically going to turn on the output 21 turned on that relay time sleep 1 means basically wait for one second then we'll turn it off turn on 1x1 wafers and again that'll keep going so if we just run this code well I'll do what air we all could press f5 and we'll go back to the board and yes that is working very nicely hopefully you can see the relays not just coming on being reflected in the back the relay as well it works quite nicely doesn't it hopefully you can hear it as well I like that it's a really good noise isn't it I just press her control C to stop this it will stop and I just come out of that just to show you can use the board numbering I've got that read nothing code as well you probably noticed that earlier this is the board numbering exactly the same piece of code except here we're using board numbering which means we're using GPIO pins 37 that 38 and 40 but if I run this again I press f5 this time cut back to the board and again hopefully our relays will cycle yes that's working absolutely fine so we prove the board works let's now think about connecting it to some mains devices so what I'm going to do now is to get our Raspberry Pi with its a mains rule a hat controlling three mains lightbulbs under here's our bulbs here and I've also got three bulb holders that fella go in there that's a bayonet holders we use it most of time in the UK and they've also got a a terminal strip and a bit of a plastic card to mattify on and a piece of wood as well and various screws and things so this is the moment that explaining computers go to all a team and MacGyver because I'm going to be doing lots of drilling and lots of screwing down and lots and lots and lots of wiring up lots and lots of wiring up and the final result will look like this here we are every home should have one shouldn't it this is our test board for running over three bulbs from the rust beep I buy this relay hat and basically we've got power coming in here this why used to be on an old iron you know it's a very short why now because most of the wires been used to do all the wiring here but basically what is going on we've got a brown is live in the UK in the main so basically live comes in there we've got three terminals with live they go into the input onto three relays over here on the relay board and the three outputs go to the three bayonet bulb holders and then blue is neutral which goes out to a three connectors for neutral which goes back out to the wire and back out to the plug and the mains again and you want to see that a bit more clearly you can see it on this circuit diagram which shows you how everything is white oh so there we are all the raring to go we just need to add the bulb so I'll do that now that should be nice than that straightforward just put these into the holders like that and so I just get this all that connected up and running we can go back into raspbian and rerun our code from previously and hopefully yes sorry I was just a bit surprised there it's a it's working it's difficult to get the lighting right on this because clearly these are quite bright bulbs I've chosen a black background for once but it works it works really well I love the noise I love the bow going old enough it's it's always great to use a Raspberry Pi to control LEDs to turn them on and off but there's something a lot more visceral about using a Raspberry Pi to control mains bulb mains appliances I think that's that's really cool I'm very pleased with the result of wiring up this test board now I'm sure some of you are asking yourselves what is the point of this thing what are you going to do with this setup and I could say well if you're going to one of those lightbulbs sequencing parties this could be very handy but other than that you might for example want to use the Raspberry Pi with a relay controller like this to turn light on off when you're away from home to make it look like your house is occupied you could turn on radios and things like that all types of things with these relays and to do that what I've done is taken our code from previously and altered it slightly so here we're importing two at libraries again GPIO but this time date time rather than time and again I've got the numbering setup for here I've gone back to board limberger just happened to like it I've set up the pins we still got our try finally thing going on and inside our infinite loop here it's basically saying now equals date time date time now dot time which basically means we can say if now our equals say 20 that 8 o'clock isn't it a minute is 44 it'll then turn on and that particular output turn on one of the bulbs and then if it's a 2045 overturn another one on and if it's a 2046 it'll turn them bugs off you could took all sorts of things up in here so you can turn things on and off and it's worth pointing out the PI doesn't have a real time clock but it will pick up a time from Wi-Fi you could of course always set the time yourself when you started this thing off and I point out if you run this over a long period of time over many days or even weeks it would happen at the same time each day could have all sorts of things inside here so if we run this code as press the f5 to run the thing up there it is and we'll make sure our clock stays on screen there and that will flick that to the bulbs and I put a bit of fast motion in because clearly we don't want to watch you forever but actually it's coming up to a 8:44 isn't it the first of our ones which is something is gonna happen on so let's see what happens it's exciting as the clock gets to the top is it gonna work 844 yes a bulb has come on so well now a flick forward in time through the magic cover fast-forwarding and here we are coming up to a next minute and hopefully yes the bulb has gone on and we'll a flick falling time again and here we are back in real time or not oh there we are both bulbs have gone out so clearly it works we proved we can do something useful with the relay board connected to the Raspberry Pi and I would point out was all kinds of ways of triggering GPIO pins you could use switches you could use a light sensors motion sensors things like that and we'll be looking at some of the options later in this series in this video we've seen how it's possible to control mains devices wired directly to a Raspberry Pi but what if you want to control mains devices which are spread all around your home a long distance from the PI and you don't want to have lots of main two wires sort of spread all over the place or maybe you just don't want to do all the main - I read your self well is there an answer to that and the answer of course is yes you can use wireless and so in my next video in this series we're looking at devices like this why this adapters you plug it into a socket you plug your device into this device itself and you can control it wirelessly from a raspy point but now that's it for this video having job you seen here please best title like button if you haven't subscribed please subscribe and I hope to talk to you again very soon [Music] you
Info
Channel: ExplainingComputers
Views: 220,383
Rating: undefined out of 5
Keywords: home automation, automation, Raspberry Pi, Raspberry Pi home automation, Raspberry Pi automation, mains relay HAT, Raspberry Pi relay control, mains relay, automation HAT, Python, Python timer code, timer code, Christopher Barnatt, Barnatt, Electronics-Salon, Electronics Salon relay, relay board, light bulb control, Raspberry Pi light bulb control, control light bulbs
Id: bOGltcgiXiU
Channel Id: undefined
Length: 15min 3sec (903 seconds)
Published: Sun Mar 31 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.