User avatar
tlfong01
Posts: 300
Joined: Sat Jun 02, 2018 1:43 pm
Location: Hong Kong

Re: Relay Module KY-019 5V

Tue Jul 17, 2018 3:22 am

tlfong01 wrote:
Tue Jun 19, 2018 9:24 am
Test results

Sample 1
Select High - Vin on = 1.99V, Vin off = 1.55V
Select Low - Vin on = 2.90V, Vin off = 3.45V (Rpi relay newbie's sorrow!)

Sample 2
Select High - Vin on = 1.92V, Vin off = 1.62V
Select Low - Vin on = 2.90V, Vin off = 3.27V (Rpi relay newbie's sorrow!)

Sample 3
Select High - Vin on = 1.95V, Vin off = 1.59V
Select Low - Vin on = 3.01V, Vin off = 3.42V(Rpi relay newbie's sorrow!)

Sample 4
Select High - Vin on = 1.95V, Vin off = 1.53V
Select Low - Vin on = 3.01V, Vin off = 3.49V (Rpi relay newbie's sorrow!)

Sample 5
Select High - Vin on = 1.81V, Vin off = 1.66V
Select Low - Vin on = 3.09V, Vin off = 3.47V (Rpi relay newbie's sorrow!)
Now I have done a quick and dirty circuit analysis.
Attachments
a9Rv2ho[1].jpg
a9Rv2ho[1].jpg (106.48 KiB) Viewed 658 times
I am an electronics hobbyist. I started playing with relays some weeks ago. Me finding things so confusing. Google is my friend and makes me great again!

User avatar
tlfong01
Posts: 300
Joined: Sat Jun 02, 2018 1:43 pm
Location: Hong Kong

Re: Relay Module KY-019 5V

Tue Jul 17, 2018 7:06 am

tlfong01 wrote:
Fri Jul 13, 2018 2:19 pm
tlfong01 wrote:
Fri Jul 13, 2018 7:22 am
I gave up and went back to setup my Rpi Zero W, ... :cry:
I soldered the 40 pin header, and saved a copy of the nice SparkFun pinout for reference.
I forgot that RpiZW needs a mini HDMI cable. So last weekend I went to Apliu Street Electronics Components Flea Market to get one. I also visited the DIY shop there and surprisingly found they have started selling Rpi3B+, no loner Arduino only, which indeed loses HK hobbyists' face.

Anyway now I am thinking of hooking RpiZW to the CJMCU17 (MCP23017) module which in turn drives 4 optocouplers EL817C.
Attachments
IYefnZH[1].jpg
IYefnZH[1].jpg (83.51 KiB) Viewed 644 times
Last edited by tlfong01 on Sat Jul 21, 2018 12:17 am, edited 1 time in total.
I am an electronics hobbyist. I started playing with relays some weeks ago. Me finding things so confusing. Google is my friend and makes me great again!

User avatar
tlfong01
Posts: 300
Joined: Sat Jun 02, 2018 1:43 pm
Location: Hong Kong

Re: Relay Module KY-019 5V

Tue Jul 17, 2018 8:43 am

tlfong01 wrote:
Thu Jul 12, 2018 7:08 am
I gave up and will try another new toy, a mini water pump. This 220VAC pump can immerse in a fish tank. I wonder if the fish gets electrocuted.
My new toy has arrived. So I will use KY019 to switch on/off this AC220V, 10W motor to see if any EMI comes out from it.

I though I would first do a table top dry run. But I forgot where I read that pump won't work if not in water, and auto shut off. In other words, only wet run is allowed.

The pump is also "brushless energy saving". My first thought was that a brushless DC motor is used. Second thought should be a 220VAC permanent magnet induction motor. I opened the pump to check inside, but found black asphalt seemingly buried the motor, ...
Attachments
ohvfhs5[1].jpg
ohvfhs5[1].jpg (29.98 KiB) Viewed 629 times
I am an electronics hobbyist. I started playing with relays some weeks ago. Me finding things so confusing. Google is my friend and makes me great again!

User avatar
tlfong01
Posts: 300
Joined: Sat Jun 02, 2018 1:43 pm
Location: Hong Kong

Re: Relay Module KY-019 5V

Wed Jul 18, 2018 5:11 am

tlfong01 wrote:
Tue Jul 17, 2018 7:06 am
Any way now I am thinking of hooking RpiZW to the CJMCU17 (MCP23017) module which in turn drives 4 optocouplers EL817C.
So I have setup the RpiZW. It is the first time I used miniHDMI, so I need to be very careful. (In earlier Rpi3 projects, I carelessly over bent the HDMI cable and the copper wires inside were partly broken, causing intermittent poor contacts, wasting me hours to troubleshoot and fixing. )

There is no USB hub on board, so the wireless keyboard and mouse dongles have no place to go. Only good thing is that the SD card is now inserted with face up, so I can proudly show off the pretty red/gray face of my Ultra 16GB HCI SanDisk card.

My OS is stretch 2018apr. I know this version is stable. I am not keen to get the newer 2018jun version, because I can online update any time. For the same reason Python 3.5 is good for me. I guess for my not too trendy applications, version 3.6 would cause me more trouble than version 3.5.
Attachments
b6lvHbR[1].jpg
b6lvHbR[1].jpg (111.16 KiB) Viewed 590 times
I am an electronics hobbyist. I started playing with relays some weeks ago. Me finding things so confusing. Google is my friend and makes me great again!

User avatar
tlfong01
Posts: 300
Joined: Sat Jun 02, 2018 1:43 pm
Location: Hong Kong

Re: Relay Module KY-019 5V

Wed Jul 18, 2018 7:20 am

tlfong01 wrote:
Wed Jul 18, 2018 5:11 am
My OS is stretch 2018apr, ... Python 3.5 is good for me. ...
I have checked that the RpiZW's USB hub can entertain it three most important guests: wireless keyboard dongle, mouse dongle, and 16GB USB flash drive.

I have mounted the RpiZW prototyping board on a A3 size acrylic board, squeezed all the too long connecting cables under the transparent raised floor, cable tied all movable connecting wirely, more secure than my cat can pull off. A transparent 3mm acrylic cover should make it also cat scratch proof.

Now the time has come to think about the MCP23017!

I googled "mcp23017 + relay" and found the posts by @geoffr most educational. One sad thing is that his posts are 6 years old, and more recent follow ups seemed not posted. Perhaps I should google harder.

RE: CONNECTING A 5V RELAY BOARD Postby geoffr » 2012-Sep-09 Sun 8:57 pm
viewtopic.php?t=15317#p169318

I've given up on the idea of using the GPIOs directly, and will give the I2C bus a bash. My current thinking is still to use a ULN2803 to drive relays.

This approach also allows me to create a lot more GPIOs, and I won't have to deal with the confusion caused by the crazy GPIO allocation on P1 - I only need to take pins from P1 through onto my relay board.

Using I2C, assuming I get it working, will have the added bonus that I can integrate a real time clock into my board.


RE: CONNECTING A 5V RELAY BOARD Postby geoffr » 2012-Sep-09 Sun 9:38 pm
viewtopic.php?t=15317#p169338

the approach I am taking is more complex, but it has greater longer-term expansion potential. I may want to add some AD converters later on - and there are readily available chips with I2C interfaces to do this. That is the stage where all the extra work will pay off.

RE: CONNECTING A 5V RELAY BOARD Postby geoffr » 2012-Sep-10 Mon 6:08 am
viewtopic.php?t=15317#p169653

The main reasons for deciding to try the I2C approach were the fact that I realised that with GPIOs I was going to get limited quickly by the number of pins actually available on the P1 header, and that if I wanted analogue inputs GPIOs would not help.

RE: CONNECTING A 5V RELAY BOARD Postby geoffr » 2012-Sep-10 Mon 11:15 am

I am thinking of is polling some analogue sensors,

RE: CONNECTING A 5V RELAY BOARD Postby geoffr » 2012-Sep-14 Fri 9:45 pm

The idea of using the MCP23017 is working. The level conversion from 3.3V to 5V using 2 mosfets is also working nicely.
Attachments
VDrfc0e[1].jpg
VDrfc0e[1].jpg (90.14 KiB) Viewed 570 times
I am an electronics hobbyist. I started playing with relays some weeks ago. Me finding things so confusing. Google is my friend and makes me great again!

User avatar
tlfong01
Posts: 300
Joined: Sat Jun 02, 2018 1:43 pm
Location: Hong Kong

Re: Relay Module KY-019 5V

Wed Jul 18, 2018 9:22 am

tlfong01 wrote:
Wed Jul 18, 2018 7:20 am
Now the time has come to think about the MCP23017!

I googled "mcp23017 + relay" and found the posts by @geoffr most educational.

I particularly like his approach of "starting complex, ending simple":

... the approach I am taking is more complex, but it has greater longer-term expansion potential. I may want to add some AD converters later on - and there are readily available chips with I2C interfaces to do this. That is the stage where all the extra work will pay off.

That is why I do not do the easy way of powering MCP23017 with 3V3 and direct connect it to RpiZW's 3V3 I2C signals. Instead I am doing the complex way of

  • 1. first level shifting RpiZW's 3V3 I2C signals by TSX0104E/TSX012 to 5V,
  • 2. then connect the shifted up I2C signals to 5V powered MCP23017.
TSX010n can be manually/software enable/disabled, and it is simple and flexible to expand, swap testing etc. With 5V I2C signals, it is also simple to interface with other 5V I2C devices.
Attachments
gPLgIvH[1].jpg
gPLgIvH[1].jpg (103.93 KiB) Viewed 555 times
I am an electronics hobbyist. I started playing with relays some weeks ago. Me finding things so confusing. Google is my friend and makes me great again!

User avatar
tlfong01
Posts: 300
Joined: Sat Jun 02, 2018 1:43 pm
Location: Hong Kong

Re: Relay Module KY-019 5V

Wed Jul 18, 2018 11:44 am

tlfong01 wrote:
Wed Jul 18, 2018 7:20 am
I googled "mcp23017 + relay" and found the posts by @geoffr most educational. I have summarized his ideas below.

RE: CONNECTING A 5V RELAY BOARD Postby geoffr » 2012-Sep-09 Sun 8:57 pm
viewtopic.php?t=15317#p169318

  • ... The main reasons for deciding to try the I2C approach were the fact that I realised that with GPIOs I was going to get limited quickly by the number of pins actually available on the P1 [40 pin] header
  • ... This approach also allows me to create a lot more GPIOs, and I won't have to deal with the confusion caused by the crazy GPIO allocation on P1
  • ... still to use a ULN2803 to drive relays.
  • ... I can integrate a real time clock into my board.
  • ... I may want to add some AD converters later on
  • ... I am thinking of is polling some analogue sensors

I am following @geoffr's approach - forget about RPi's GPIO pins, instead use I2C MCP23017 IO expander, and use other I2C devics like ADC, DAC, PWM, realt time clock, sensor, etc. I think I should also try SPI and UART, similar to I2C.

I still need to do some limited local GPIO output pins for demultiplexing, and input pins for interrupts. So I am renumbering the pins for easy reference.

Now I can start the I2C MCP23017 now.
Attachments
I3dcz2w[1].jpg
I3dcz2w[1].jpg (97.34 KiB) Viewed 540 times
I am an electronics hobbyist. I started playing with relays some weeks ago. Me finding things so confusing. Google is my friend and makes me great again!

User avatar
tlfong01
Posts: 300
Joined: Sat Jun 02, 2018 1:43 pm
Location: Hong Kong

Re: Relay Module KY-019 5V

Wed Jul 18, 2018 1:35 pm

tlfong01 wrote:
Wed Jul 18, 2018 11:44 am
I still need to do some limited local GPIO output pins for demultiplexing, and input pins for interrupts. So I am renumbering the pins for easy reference.
I have made more changes to the pin numbering. I have also assigned the grounds as sub grounds. This will hopefully reduce grounding interference.
Attachments
Ib7QNB3[1].jpg
Ib7QNB3[1].jpg (144.58 KiB) Viewed 524 times
I am an electronics hobbyist. I started playing with relays some weeks ago. Me finding things so confusing. Google is my friend and makes me great again!

User avatar
tlfong01
Posts: 300
Joined: Sat Jun 02, 2018 1:43 pm
Location: Hong Kong

Re: Relay Module KY-019 5V

Wed Jul 18, 2018 2:13 pm

Mortimer wrote:
Fri Jul 13, 2018 12:31 pm
... switching inductive mains loads may causing arcing between the relay contacts,
... inductively coupled back ... and affect the controller or computer.
... Some sort of snubber network might be considered.
... research "snubber" and "inductive load arc suppression".
... snubber topic, can also cover the coil side of a relay too
Yes, I think the EMI thing is quite complicated. I googled a bit and found the following article interesting. The scope captures are impressive. This is what I hope to see when switching my little motors.

Protecting Inputs in Digital Electronics - Digi-Key 2012-04-11
https://www.digikey.com/en/articles/tec ... lectronics

In a generic electronic system there are some inputs that are controlled by the end user. These inputs are read by electronics and acted upon by using outputs. The inputs can come from a myriad of sources: buttons, switches, sensors, relays, and communication devices, to name a few.

In certain environments and situations, these input signals can pose a threat to the electronics reading them – especially if those electronics are designed without thought of protection.

In a typical factory system there may be buttons on a control panel located remotely from the central processing unit. The buttons are connected to central processing via long wires. Unfortunately this can lead to inadvertent electronic failure. Long wires can act as an inductor and when a button is opened or closed, large voltage spikes can show up on the electronic paths. ...

Using this model, it is easy to see the problems with unprotected inputs. Any large voltage that shows up on input pin is presented directly to the interior electronics (microcontroller). Regardless of how this voltage is produced (ESD, induced EMI, switch closure, user error), this can damage the microcontroller, and perhaps cause the entire system to fail.
Last edited by tlfong01 on Mon Jul 23, 2018 12:55 pm, edited 1 time in total.
I am an electronics hobbyist. I started playing with relays some weeks ago. Me finding things so confusing. Google is my friend and makes me great again!

User avatar
tlfong01
Posts: 300
Joined: Sat Jun 02, 2018 1:43 pm
Location: Hong Kong

Re: Relay Module KY-019 5V

Wed Jul 18, 2018 2:20 pm

Mortimer wrote:
Fri Jul 13, 2018 1:09 pm
You might find your motor is small enough that any arcing is minimal and unlikely to give you problems.
Yes, I don't expect much EMI from my little motors (permanent magnet induction motor 4W, water pump 10W). But I might gradually increase the power to an electric fan, and perhaps in a years time, finally to control an 1,500W induction cooker.

I have recently bought some 220VAC 15W LED lamps. They use transformers (I guess from 220VAC to 12/24DAC) and say they are flicker free. I am interested to see how flicker free they are. Some years back I bought some cheap LED lamps and found the transformer very warm, with eye soaring flickers. I hope the LED lamp technology has advanced by now.
I am an electronics hobbyist. I started playing with relays some weeks ago. Me finding things so confusing. Google is my friend and makes me great again!

User avatar
Mortimer
Posts: 881
Joined: Sun Jun 10, 2012 3:57 pm

Re: Relay Module KY-019 5V

Wed Jul 18, 2018 2:30 pm

tlfong01 wrote:
Wed Jul 18, 2018 2:20 pm
I have recently bought some 220VAC 15W LED lamps. They use transformers (I guess from 220VAC to 12/24DAC) and say they are flicker free.
Unlikely, almost always capacitive-dropper circuits nowadays. BigClive on Youtube does some interesting tear-downs of this sort of thing and reverse engineers the circuits, so you get to see a circuit diagram of typical electrical items. One of his tear-downs of an LED bulb is here : https://youtu.be/a-exD1-6gsY

User avatar
rpdom
Posts: 12659
Joined: Sun May 06, 2012 5:17 am
Location: Ankh-Morpork

Re: Relay Module KY-019 5V

Wed Jul 18, 2018 6:43 pm

Mortimer wrote:
Wed Jul 18, 2018 2:30 pm
tlfong01 wrote:
Wed Jul 18, 2018 2:20 pm
I have recently bought some 220VAC 15W LED lamps. They use transformers (I guess from 220VAC to 12/24DAC) and say they are flicker free.
Unlikely, almost always capacitive-dropper circuits nowadays. BigClive on Youtube does some interesting tear-downs of this sort of thing and reverse engineers the circuits, so you get to see a circuit diagram of typical electrical items. One of his tear-downs of an LED bulb is here : https://youtu.be/a-exD1-6gsY
Big Clive was tearing down a 6W bulb. That is about the limit for a capacitive-dropper before the components get too big or too hot. Most higher power lamps use some sort of SMPS.

User avatar
tlfong01
Posts: 300
Joined: Sat Jun 02, 2018 1:43 pm
Location: Hong Kong

Re: Relay Module KY-019 5V

Thu Jul 19, 2018 8:29 am

Mortimer wrote:
Wed Jul 18, 2018 2:30 pm
tlfong01 wrote:
Wed Jul 18, 2018 2:20 pm
I have recently bought some 220VAC 15W LED lamps. They use transformers (I guess from 220VAC to 12/24DAC) and say they are flicker free.
Unlikely, almost always capacitive-dropper circuits nowadays. BigClive on Youtube does some interesting tear-downs of this sort of thing and reverse engineers the circuits, so you get to see a circuit diagram of typical electrical items. One of his tear-downs of an LED bulb is here : https://youtu.be/a-exD1-6gsY

Ah, I never heard of capacitive-dropper circuits.

I only know two types of 220VAC to low voltage DC power supplies. One type has a transformer and a regulated voltage device (Less than 10 years ago I still used a power BJT NPN called 2N3055 to assemble a regulator, because I found it scary using inductor based LM2596 (I actually bought a couple of LM2596s but never tried it) .

The other is switching power supply which has a oscillating switch, switching on/off to charge/discharge a capacitor to get the desired voltage,...

I guess my power supply knowledge is perhaps 20 years out of date. I need to google to catch up.

I neither heard of BigClive. I watched the video and found it very interesting.

I think he is a very efficient teacher, teaching you things and at the same time entertaining you.

He is not telling any jokes, but he reminds me of Mr Bean.

He made me laugh in the beginning, trying to open the bulb by hand, though like Mr Bean, failed, but then I knew the bulb is make of plastic, ...

At the end, he took out a ugly, stupid looking calculator to do the arithmetic. I closely watched with amusement his perhaps 50 years old ugly calculator and paid no attention what he was calculating.

His other tool, a box with big Brown Green Blue buttons also surprised me. I have never seen such a thing before. My first guess is a mains simulator, using battery and can generate 200VAC but low current, for testing.

Actually I bought such a toy, called an inverter, and hope to simulate mains 200VAC for my experiments.

I have neither seen an electrician using a tool to check the phase angle (or power factor, I forgot). I know what is mains phase, but don't know why he need to check power factor while studying the lamp.

In short, he is telling me many things I don't know. So I am happy to have learned so many new things from him.

I should watch Big Clive's other videos soon.
Last edited by tlfong01 on Sat Jul 21, 2018 4:40 am, edited 2 times in total.
I am an electronics hobbyist. I started playing with relays some weeks ago. Me finding things so confusing. Google is my friend and makes me great again!

User avatar
tlfong01
Posts: 300
Joined: Sat Jun 02, 2018 1:43 pm
Location: Hong Kong

Re: Relay Module KY-019 5V

Thu Jul 19, 2018 8:46 am

rpdom wrote:
Wed Jul 18, 2018 6:43 pm
Big Clive was tearing down a 6W bulb. That is about the limit for a capacitive-dropper before the components get too big or too hot. Most higher power lamps use some sort of SMPS.
Just now I wikied and also read the following article.

What’s all this stuff about capacitive power supplies for LEDs? 2017jun14 L Teschler
https://www.powerelectronictips.com/wha ... lies-leds/

I skipped all the mathematics. I always find calculus, dx/dt etc scary. Anyway, I guess this type of circuits is invented, or getting popular, mainly for LED lamps. No wonder I have not heard of it before. I have not heard of SMPS either. Perhaps I should google that later.

update 2018jul19hkt2104
I googled and found SMPS stands for Switch Mode Power Supply. I used to call it switching PSU.
I am an electronics hobbyist. I started playing with relays some weeks ago. Me finding things so confusing. Google is my friend and makes me great again!

User avatar
rpdom
Posts: 12659
Joined: Sun May 06, 2012 5:17 am
Location: Ankh-Morpork

Re: Relay Module KY-019 5V

Thu Jul 19, 2018 5:17 pm

tlfong01 wrote:
Thu Jul 19, 2018 8:29 am
I closely watched his perhaps 50 years old ugly calculator and forgot what he was saying.
There were no calculators like that 50 years ago. There were some purely mechanical ones. Some worked by pulling a lever to do the calculation. Others used electricity to drive a motor to do the work.
His other tool, a box of Brown Green Blue buttons also surprised me. I have never seen such thing before, my first guess is a mains simulator, uses battery but generate 200 voltage but low current, for testing.
Ah, the Quicktest box. It is not a mains simulator. It is a quick and (fairly) safe way of connecting test circuits to mains power. You clip the wires into the connectors and the power is off until you close the lid.

Not easy to get hold of now.

User avatar
tlfong01
Posts: 300
Joined: Sat Jun 02, 2018 1:43 pm
Location: Hong Kong

Re: Relay Module KY-019 5V

Fri Jul 20, 2018 3:40 am

rpdom wrote:
Thu Jul 19, 2018 5:17 pm
tlfong01 wrote:
Thu Jul 19, 2018 8:29 am
I closely watched his perhaps 50 years old ugly calculator ...
There were no calculators like that 50 years ago. ...
But 50 years ago Little Clive was already selling his little, pocket size calculator. So Big Clive's big, notebook size calculator must be over 50 years old. Or have I been reading Google's fake old news?
I am an electronics hobbyist. I started playing with relays some weeks ago. Me finding things so confusing. Google is my friend and makes me great again!

User avatar
rpdom
Posts: 12659
Joined: Sun May 06, 2012 5:17 am
Location: Ankh-Morpork

Re: Relay Module KY-019 5V

Fri Jul 20, 2018 5:18 am

tlfong01 wrote:
Fri Jul 20, 2018 3:40 am
rpdom wrote:
Thu Jul 19, 2018 5:17 pm
tlfong01 wrote:
Thu Jul 19, 2018 8:29 am
I closely watched his perhaps 50 years old ugly calculator ...
There were no calculators like that 50 years ago. ...
But 50 years ago Little Clive was already selling his little, pocket size calculator. So Big Clive's big, notebook size calculator must be over 50 years old. Or have I been reading Google's fake old news?
:lol:
Little Clive's first slim pocket calculator was made in 1972. Just 46 years ago. I don't remember that one. At around £80 it wasn't something many people could afford. That would be the equivalent of about £1000 today. The first electronic calculator I saw was a few years after that.

Big Clive's calculator probably cost under £10 at current prices.

It is amazing how they have changed.

User avatar
tlfong01
Posts: 300
Joined: Sat Jun 02, 2018 1:43 pm
Location: Hong Kong

Re: Relay Module KY-019 5V

Fri Jul 20, 2018 9:35 am

rpdom wrote:
Fri Jul 20, 2018 5:18 am
Little Clive's first slim pocket calculator 46 years ago ...
at around £80 it wasn't something many people could afford...
equivalent of about £1000 today...
Ah, I guess 50 years ago even a notebook size calculator might cost many pounds and only big guys like Big Clive could afford. Now I see his stupid calculator no longer ugly, but an elegant piece of art. And by the way, selling calculators £1,000 each, no wonder Sir Sinclair is so very rich!

Anyway, no more those were the days dreaming. I have waken back to the cruel reality and carrying on my hard work with the poorman's calculator, Rpi Zero W.
Attachments
8I1F4QL[1].jpg
8I1F4QL[1].jpg (160.57 KiB) Viewed 351 times
Last edited by tlfong01 on Sat Jul 21, 2018 1:40 am, edited 2 times in total.
I am an electronics hobbyist. I started playing with relays some weeks ago. Me finding things so confusing. Google is my friend and makes me great again!

User avatar
Mortimer
Posts: 881
Joined: Sun Jun 10, 2012 3:57 pm

Re: Relay Module KY-019 5V

Fri Jul 20, 2018 12:18 pm

I have one of these in a drawer at home. My Father bought it way back in the 70's:

Image
Sinclair Cambridge Memory (type 1)


It stills works... mostly. If I recall a segment of the highest order digit has died.

User avatar
tlfong01
Posts: 300
Joined: Sat Jun 02, 2018 1:43 pm
Location: Hong Kong

Re: Relay Module KY-019 5V

Fri Jul 20, 2018 3:02 pm

tlfong01 wrote:
Fri Jul 20, 2018 9:35 am
Anyway, no more those were the days. I have come back to reality and resumed playing with the poorman's calculator, the Rpi Zero W.

Now I plan to test MCP23017 in 2 big steps.

  • 1. Test RTC3231 Real Time Clock
  • 2. Test MCP23017 IO Expander
I have connected Rpi Zero W 3.3V I2C (SCL, SDA) to logical level converter TXS0104E which converts 3.3V I2C to 5V I2C, to input to RTC DS3231.

Terminal command $i2cdetect -y 1 display RTC and EEPROM addresses 0X68, and 0X57

So far so good, next step is to connect and detect MCP23017
Attachments
HerC7R1[1].jpg
HerC7R1[1].jpg (90.73 KiB) Viewed 328 times
I am an electronics hobbyist. I started playing with relays some weeks ago. Me finding things so confusing. Google is my friend and makes me great again!

User avatar
tlfong01
Posts: 300
Joined: Sat Jun 02, 2018 1:43 pm
Location: Hong Kong

Re: Relay Module KY-019 5V

Sat Jul 21, 2018 4:33 am

tlfong01 wrote:
Fri Jul 20, 2018 3:02 pm
Now I plan to test MCP23017 in 2 big steps.
  • 1. Test RTC3231 Real Time Clock
  • 2. Test MCP23017 IO Expander
So far so good, next step is to connect and detect MCP23017

Now the RpiZW I2c signal flows as summarized below.

1. Rpi 3.3V I2C goes into TSX0104E which outputs 5V I2c
2. TSX0104E output 5V I2c goes to DS3231 and MCP23017

i2cdetect -y 1 gives expected addresses: 0X57, 0x68, and 0x22 (Mcp23017 sub add 2)

So far so good. Next step to think about what to do next.
Attachments
dykMDah[1].jpg
dykMDah[1].jpg (80.74 KiB) Viewed 307 times
I am an electronics hobbyist. I started playing with relays some weeks ago. Me finding things so confusing. Google is my friend and makes me great again!

User avatar
DougieLawson
Posts: 33362
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Relay Module KY-019 5V

Sat Jul 21, 2018 9:40 am

Here's a collection of functions to make driving your MCP23017 easier. It starts by setting both direction registers to outputs on all 16 GPIOs. You may need to swap pinOn and pinOff if your relays are active LOW.

Code: Select all

import smbus

ADDR = 0x22
IODIRA = 0x00
IODIRB = 0x01
GPIOA = 0x12
GPIOB = 0x13
OLATA = 0x14
OLATB = 0x15

bus=smbus.SMBus(1)

bus.write_byte_data(ADDR,IODIRA,0x00)
bus.write_byte_data(ADDR,IODIRB,0x00)
global valueA
global valueB
valueA=0
valueB=0

def pinOn(bank, pin):
#  print (bank,  pin, "== ON")
  global valueA
  global valueB
  bit = pin - 1
  if bank == 'B':
    valueB = valueB | (1 << bit)
    bus.write_byte_data(ADDR, OLATB, valueB)
  else:
    valueA = valueA | (1 << bit)
    bus.write_byte_data(ADDR, OLATA, valueA)

def pinOff(bank, pin):
#  print (bank, pin, "== OFF")
  global valueA
  global valueB
  bit = pin - 1
  if (bank == 'B'):
    valueB = valueB & (0xff - (1 << bit))
    bus.write_byte_data(ADDR, OLATB, valueB)
  else:
    valueA = valueA & (0xff - (1 << bit))
    bus.write_byte_data(ADDR, OLATA, valueA)

def pinStatus(bank, pin):
  global valueA
  global valueB
  bit = pin - 1
  if (bank == 'B'):
    state = ((valueB&(1<<bit))!=0)
  else:
    state = ((valueA&(1<<bit))!=0)
  return state

def pinAllOff():
#  print "All off called"
  global valueA
  global valueB
  valueA = 0xFF
  bus.write_byte_data(ADDR, OLATA, valueA)
  valueB = 0xFF
  bus.write_byte_data(ADDR, OLATB, valueB)
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

User avatar
tlfong01
Posts: 300
Joined: Sat Jun 02, 2018 1:43 pm
Location: Hong Kong

Re: Relay Module KY-019 5V

Sat Jul 21, 2018 12:57 pm

DougieLawson wrote:
Sat Jul 21, 2018 9:40 am
Here's a collection of functions to make driving your MCP23017 easier. It starts by setting both direction registers to outputs on all 16 GPIOs. You may need to swap pinOn and pinOff if your relays are active LOW.
...
Many thanks for your MCP23017 functions. They came just in time. I quickly skimmed through and found at least one programming trick I heard of but never used. I think I need to google and study for one or two hours to fully understand all the functions.

I have numbered the statements in case I need to ask you something. Your functions using the shift and logical operators <<, |, &, ! reminded me when I first studied some manufacturers' example code on SPI and I2C devices. These operators, together with 7/8 bit I2C addresses, also SPI operation modes 00, 01 etc actually took me perhaps 100 hours to clarify everything. I still remember my puzzle when studying the SPI code on output, but input is unnecessarily or redundantly handled. Another thing is that for I2C, one buffer can have input and output at the same physical point.

I have clarified all the logical and shift operators, I2C and SPI. But as I said, I might need to study all the programming tricks you used, before I am comfortable to use or modify.
Attachments
n0p2kVz[1].jpg
n0p2kVz[1].jpg (156.25 KiB) Viewed 278 times
I am an electronics hobbyist. I started playing with relays some weeks ago. Me finding things so confusing. Google is my friend and makes me great again!

User avatar
tlfong01
Posts: 300
Joined: Sat Jun 02, 2018 1:43 pm
Location: Hong Kong

Re: Relay Module KY-019 5V

Sat Jul 21, 2018 1:52 pm

tlfong01 wrote:
Sat Jul 21, 2018 4:33 am
i2cdetect -y 1 gives expected addresses: 0X57, 0x68, and 0x22 (Mcp23017 sub add 2)
So far so good. Next step to think about what to do next.
Now I am going to program the IO Expander MCP23017 and Real Time Clock DS3231 together.

My long term plan is to control up to 64 relays for home automation. It is very likely that later I will add ADC and DAC with various sensors. I need to think very thoroughly before I proceed. One thing I need to do is to update and upgrade my Python programming techniques.

Some years back I learned Python 2.7 from the MIT OCW course listed below.

Introduction to Electrical Engineering and Computer Science I
https://ocw.mit.edu/courses/electrical- ... /index.htm

Just now I visited the MIT site again and found that they seemed to have revised something. The materials I wish to revise might not be there, or moved to somewhere else. I also visited the Standford Python site. Unfortunately they don't have the videos. So I can only browsed the slides.

Standford Python - CS41 happy code the python programming language
http://stanfordpython.com/#lecture

On second thought, I think I might just use the official Python site python.org and select the topics I wish to learn, guided by the MIT and Stanford course outlines.

Official Python Site
https://www.python.org/

/ to continue, ...
I am an electronics hobbyist. I started playing with relays some weeks ago. Me finding things so confusing. Google is my friend and makes me great again!

User avatar
Mortimer
Posts: 881
Joined: Sun Jun 10, 2012 3:57 pm

Re: Relay Module KY-019 5V

Sat Jul 21, 2018 4:52 pm

You could try this...
https://www.codecademy.com

Return to “Automation, sensing and robotics”

Who is online

Users browsing this forum: Google [Bot] and 18 guests