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

MCP23017 Learning Notes

Wed Jan 23, 2019 12:44 pm

...
Introduction

I am doing a smart home project, using many DIY components including solenoid valve. I have learnt how to use a Rpi GPIO pin to control a relay (KY019) which in turn switches a solenoid valve (Puro-XD-12) [1], [2].

Now I am thinking of building a small musical fountain in my roof top garden, with more water features than Rpi GPIO can handle. I am starting with 64 water jets controlled by 64 GPIO pins. One way is to use 4 MCP23017s, each with 16 GPIO pins.


I have just bought a couple of MCP23017 modules. Next step is to put one module on a prototyping board, as a testing rig.

[1] Puro-XD-12 Solenoid Valve Schematic
https://www.raspberrypi.org/forums/view ... 4#p1418278

[2] Rpi Python Program to Switch Solenoid Valve Puro-XD-12
https://www.raspberrypi.org/forums/view ... 4#p1418976

/ to continue, ...
...
Attachments
mcp23017_2019jan2302.jpg
mcp23017_2019jan2302.jpg (182.45 KiB) Viewed 2947 times
Last edited by tlfong01 on Thu Jan 31, 2019 5:59 am, edited 4 times in total.
I am an electronics and smart home hobbyist.

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

Re: Cat Home DIY Q&A - Solenoid MCP23017 How To

Wed Jan 23, 2019 1:17 pm

tlfong01 wrote:
Wed Jan 23, 2019 12:44 pm
I have just bought a couple of MCP23017 modules. Next step is to put one module on a prototyping board, to make a testing rig.

MCP23017 Intermittent Instability Problem

I have been playing with MCP23017 in a couple of previous projects, but each time I found intermittent instability problem [1]. I suspect one cause of the problem is that I used very cheap modules which might be reject items. This time I am using an expensive (¥13!) version from a reputable TaoBao shop, hoping that the problem will disappear.

To save time and also verify that it was not the poor wiring that caused the problem, I am using the old prototyping board, replacing the problematic mcp20317 module by the new module. The old board is attached below.


MCP23017 Intermittent Instability Problem in an Old Project
https://www.raspberrypi.org/forums/view ... 5#p1400599
...
Attachments
mcp23017_old_board_2019jan2301.jpg
mcp23017_old_board_2019jan2301.jpg (156.54 KiB) Viewed 2939 times
Last edited by tlfong01 on Thu Jan 24, 2019 1:35 am, edited 2 times in total.
I am an electronics and smart home hobbyist.

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

Re: Cat Home DIY Q&A - Solenoid MCP23017 How To

Wed Jan 23, 2019 2:30 pm

tlfong01 wrote:
Wed Jan 23, 2019 1:17 pm
To save time and also verify that it was not the poor wiring that caused the problem, I am using the old prototyping board, replacing the problematic mcp20317 module by the new module.

MCP23017 Module Minimum Wired Completed for Testing

Now I have soldered the Dupont male connectors on the module, and completed the minimum wiring for preliminary testing.

Next step is connecting Rpi I2C cable to the module and start testing. But I have forgotten which cable is which on the Rpi side. So I need to read the old posts to refresh my aging memory. :mrgreen:
...
Attachments
mcp23017_wiring_2019jan2301.jpg
mcp23017_wiring_2019jan2301.jpg (180.01 KiB) Viewed 2916 times
Last edited by tlfong01 on Fri Jan 25, 2019 12:46 am, edited 1 time in total.
I am an electronics and smart home hobbyist.

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

Re: Cat Home DIY Q&A - Solenoid MCP23017 How To

Wed Jan 23, 2019 2:51 pm

Attachments
i2c_cabling_2019jan2302.jpg
i2c_cabling_2019jan2302.jpg (193.16 KiB) Viewed 2907 times
I am an electronics and smart home hobbyist.

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

Re: Cat Home DIY Q&A - Solenoid MCP23017 How To

Thu Jan 24, 2019 4:44 am

tlfong01 wrote:
Wed Jan 23, 2019 2:51 pm
Next step is connecting Rpi I2C cable to the module and start testing.

Testing MCP23017 Status LEDs

Before the Rpi python testing, I need to first do a manual test of the status LEDs, to make sure they are wired correctly.

Just now I applied 5V to the 8 push pull, high level activate, Port A status LEDs and the 8 Port B LEDs. I found one of the 16 LEDs not on. I guess it is burnt.

Anyway, I then applied ground to the pull up, low activate, interrupt status LEDs and found both green and white LED on. But after a couple of seconds, I smelled plastic burning :( , and found the green LED off. So the green LED was also fried. I am not sure if the cheapy LEDs I bought are reject items again, or I used the wrong current limiting resistors.


So I searched my old posts to refresh my memory.

Old posts on pullup resistor and LED
[1] Fake pullup resistor - https://www.raspberrypi.org/forums/view ... 5#p1400957
[2] 2N2220 driven LED - https://www.raspberrypi.org/forums/view ... 5#p1401800
[3] MCP23017 driven LED - https://www.raspberrypi.org/forums/view ... 5#p1404009
[4] Blue LED - https://www.raspberrypi.org/forums/view ... 5#p1404020

For my MCP23017 projects, I am using 3mm rectangular LEDs, with the following current limiting resistors,

Red = 1k2, Yellow = 560R, Green = 4k7, Blue = 1k2.

High Efficiency Red (Iv (mcd) @10mA = 7)
Yellow (Iv (mcd) @10mA = 4)
Green (Iv (mcd) @10mA = 30)
Blue (Iv (mcd) @10mA = 55)


Attachments
mcp23017_led_2019jan1401.jpg
mcp23017_led_2019jan1401.jpg (145.55 KiB) Viewed 2858 times
Last edited by tlfong01 on Fri Jan 25, 2019 3:33 am, edited 1 time in total.
I am an electronics and smart home hobbyist.

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

Re: Cat Home DIY Q&A - Solenoid MCP23017 How To

Thu Jan 24, 2019 6:56 am

tlfong01 wrote:
Thu Jan 24, 2019 4:44 am
Next step is connecting Rpi I2C cable to the module and start testing.
Testing MCP23017 Status LEDs
Before the Rpi python testing, I need to first do manual test of the status LEDs, to make sure they are wired correctly.
I smelled plastic burning, and found the green LED off. So the green LED was fried. I am not sure if the cheapy LEDs I bought are reject items again, or I used the wrong current limiting resistors.

Detecting MCP23017

I ignored the fried LED and moved on to test the MCP23017.

But then I found white smoke coming out from the MCP23017, again with a plastic burning smell.

This is weird, because I did not connect any load to the GPIO, which also should be power on reset to input mode.

And I found i2cdetect could not find the MCP23017. So I knew very likely it was burnt, for unknown reason. Anyway, I replaced a new MCP23017 module and found i2cdetect can detect it now.

Smoke Gets In Your Eyes - The Platters 5,206,353 views
https://www.youtube.com/watch?v=H2di83WAOhU

Next step is try to ping the MCP23017.

...

Code: Select all

KY019 Posts Contents 2019jan24hkt1609

https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=77158&start=175#p1341737 (Rpi Zero Setup)
https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=77158&start=175#p1342121 (Rpi ZeroW Setup)
https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=77158&start=175#p1342143 (Rpi ZeroW Setup)
https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=77158&start=175#p1342224 (Rpi ZeroW Pinout)
https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=77158&start=175#p1342280 (Rpi ZeroW Pinout)
https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=77158&start=175#p1342167 (RpiZW + TSX0104 + MCP23017)
https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=77158&start=175#p1343205 (RpiZW + TSX0104 + MCP23017)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=200#p1343814 (RpiZ + MCP23017 Schematic)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=200#p1344027 (SMBus Reading Notes)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=200#p1344180 (SMBus User Guide)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=200#p1344493 (Rpi + TSC0104 + EL817C + ULN2803 Schematic)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=200#p1344684 (MCP23017 Write Read Register MCVE Program V1.4, V1.5)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=225#p1345103 (DS3231 Write Read Register Program)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=225#p1345488 (RTC DS3231 Test Program)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=225#p1345922 (Rpi + TSX0104 + Relay + Solenoid + Heater Schematic)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=250#p1346190 (RTC Program v1.7)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=250#p1346866 (set up RpiZero (no WiFi))
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=250#p1347291 (back up SD card)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=250#p1348064 (MCP23017 Controlling DHT22 - FAIULURE!!!)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=275#p1350076 (RTC Alarm Clock Test Program)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=300#p1351394 (I2C Write Block Function)
https://www.raspberrypi.org/forums/viewtopic.php?f=29&t=219675&p=1348886#p1348848 (I2C Speed Setting)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=325#p1352292 (TSX0101 Level Shifting Notes)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=325#p1352373 (I2C long cables, extender) 
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=325#p1353204 (HDC0180 Temperature Sensor)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=350#p1353982 (HDC0180 Temperature Humidity Program)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=350#p1355575 (SMBus User Guide)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=350#p1356156 (RpiZero Pinout)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=375#p1356510 (DBS0180 notes)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=375#p1357003 (I2C Extender P82B715)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=375#p1362527 (Detecting 8 MCP23017)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=400#p1367365 (PCA9619 I2C Buffer)

https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=400#p1374955 (Testing 200VAC Water Pump)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=400#p1374955 (Sumberged water Pump Grounding Problem)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=425#p1381269 (Testing 12V Water Pump)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=425#p1381407 (Hall Effect Flow Sensor)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=425#p1381664 (Testing 12V Water Pump)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=425#p1381718 (Hall Effect Sensor Flow Meter Test Results)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=450#p1400473 (Real and Fake KY019 Relays)
https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=475#p1404713 (Bath Room Scale Tearing Down Notes)

https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=77158&start=475#p1419852 (End of 20 pages, 489 KY019 Posts)

.END
...
Attachments
mcp23017_detect_2019jan2401.jpg
mcp23017_detect_2019jan2401.jpg (180.17 KiB) Viewed 2846 times
Last edited by tlfong01 on Fri Jan 25, 2019 3:36 am, edited 2 times in total.
I am an electronics and smart home hobbyist.

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

Re: Cat Home DIY Q&A - Solenoid MCP23017 How To

Thu Jan 24, 2019 7:20 am

tlfong01 wrote:
Thu Jan 24, 2019 6:56 am
But then I found white smoke coming out from the MCP23017, again with a plastic burning smell
This is weird, because I did not connect any load to the GPIO, which also should be power on reset to input mode.
And I found i2cdetect could not find the MCP23017. So I knew very likely it is burnt, for unknown reason. Anyway, I replaced a new one and found i2cdetect can detect it now.

MCP23017 Postmortem

I checked the burnt spot MCP23017 and found it around Pins 9, 10, (Vdd, Vss). So I guess some pins should have been shorted somewhere. But it is difficult to investigate further. So I stopped here, reminding myself to check open short carefully next time before power on.
...
Attachments
m017_postmortem_2019jan2401.jpg
m017_postmortem_2019jan2401.jpg (192.97 KiB) Viewed 2838 times
I am an electronics and smart home hobbyist.

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

Re: Cat Home DIY Q&A - Solenoid MCP23017 How To

Thu Jan 24, 2019 8:30 am

tlfong01 wrote:
Thu Jan 24, 2019 6:56 am

MCP23017 Testing - Writing and Reading A Register

Now I am writing a python program to write something to a MCP23017 register and read it back.

Old Post for Reference
MCP23017 IOX Write Read Register V1.4
https://www.raspberrypi.org/forums/view ... #p1344684
DS3231 RTC Write Read Register V1.5
https://www.raspberrypi.org/forums/view ... 5#p1345103
I am an electronics and smart home hobbyist.

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

Re: Cat Home DIY Q&A - Solenoid MCP23017 How To

Thu Jan 24, 2019 12:58 pm

tlfong01 wrote:
Thu Jan 24, 2019 8:30 am
MCP23017 Testing - Writing and Reading A Register

RTC DS3231 Ping/Hello/WriteReadRegister Program

I read my old posts on MCP23017 ping program but found another similar program on RTC DS3231. This reminds me that at that time I found MCP23017 unstable, often not detectable by command "i2cdetect -y 1". So I instead first wrote the DS3231 ping program. The reason is that if I found MCP23017, and I was not sure if the I2C thing is working properly, I would use RTC DS3231 to verify if everything OK. Because I would use real time clock sooner or later any way, it is a good idea to entertain DS3231 first.

And I worry that if I start DS3231 in this thread, I would again make this thread unnecessarily long again.

So I am opening a new topic called,


Cat Home DIY Q&A - Real Time Clock How To
I am an electronics and smart home hobbyist.

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

Re: Cat Home DIY Q&A - Solenoid MCP23017 How To

Thu Jan 24, 2019 2:56 pm

tlfong01 wrote:
Thu Jan 24, 2019 12:58 pm
So I am opening a new topic called Cat Home DIY Q&A - Real Time Clock How To

Python Program to Ping DS3231 and MCP23017

I opened the new topic on RTC and listed a ping RTC program there.
https://www.raspberrypi.org/forums/view ... 1#p1420824

Then I wrote another program that pings both RTC DS3231 and IOX MCP23017. The listing is given below.

...

Code: Select all

# rtcIox01 v1.65 tlfong01 2019jan24hkt2245

# Rpi3B+ Raspbian stretch linux 4.14.34-v7+ arm python3.5.3

# Program Description
# Ping DS3231 and MCP23017 by writing and then reading a register

import smbus
i2cCh = smbus.SMBus(1)

# DS3231 Config

rtcDevAddrByte = 0x68 # DS3231 I2C Device Address
rtcRegAddrByte = 0x03 # DS3231 Day Register (only lower nibble can write/read)

# MCP23017 Config
ioxDevAddrByte = 0x20 # MCP23017 I2C Device Address
ioxRegAddrByte = 0x00 # MCP23017 IO Direction Register Address


# *** Read Write Print Device/Register Functions ***

def writeDviTwoBytes(i2cPort, devAddrByte, dataByte1, dataByte2):
    i2cPort.write_byte_data(devAddrByte, dataByte1, dataByte2)
    return

def readDvRegOneByte(i2cPort, devAddrByte, regAddrByte):
    readByte = i2cPort.read_byte_data(devAddrByte, regAddrByte)
    return readByte

def writeDvRegOneByte(i2cPort, devAddrByte, regAddrByte, writeByte):
    writeDviTwoBytes(i2cPort, devAddrByte, regAddrByte, writeByte)
    return

def printDvRegOneByte(i2cPort, devAddrByte, regAddrByte, printTitle):
    readByte = readDvRegOneByte(i2cPort, devAddrByte, regAddrByte)
    print(printTitle, hex(readByte))

# *** Test Functions ***

def pingDs3231(testByte):
    devAddrByte = rtcDevAddrByte
    regAddrByte = rtcRegAddrByte
    printDvRegOneByte(i2cCh, devAddrByte, regAddrByte, 'Byte Read    =')
    writeDvRegOneByte(i2cCh, devAddrByte, regAddrByte, testByte)
    print('Byte Written =', hex(testByte))
    printDvRegOneByte(i2cCh, devAddrByte, regAddrByte, 'Byte Read    =')

def pingMcp23017(testByte):
    devAddrByte = ioxDevAddrByte
    regAddrByte = ioxRegAddrByte
    printDvRegOneByte(i2cCh, devAddrByte, regAddrByte, 'Byte Read    =')
    writeDvRegOneByte(i2cCh, devAddrByte, regAddrByte, testByte)
    print('Byte Written =', hex(testByte))
    printDvRegOneByte(i2cCh, devAddrByte, regAddrByte, 'Byte Read    =')
    
def testPingDs3231():
    print('\nPing DS3231 Twice, ...')
    print('Ping #1')
    pingDs3231(0x5)
    print('Ping #2')
    pingDs3231(0x7)    
    return

def testPingMcp23017():
    print('\nPing Mcp23017 Twice, ...')
    print('Ping #1')
    pingMcp23017(0x66)
    print('Ping #2')
    pingMcp23017(0x88)    
    return

# *** Main ***

testPingDs3231()
testPingMcp23017()

# *** Sample Output ***

'''
Ping DS3231 Twice, ...
Ping #1
Byte Read    = 0x7
Byte Written = 0x5
Byte Read    = 0x5
Ping #2
Byte Read    = 0x5
Byte Written = 0x7
Byte Read    = 0x7

Ping Mcp23017 Twice, ...
Ping #1
Byte Read    = 0x88
Byte Written = 0x66
Byte Read    = 0x66
Ping #2
Byte Read    = 0x66
Byte Written = 0x88
Byte Read    = 0x88
'''

# *** End ***
I am an electronics and smart home hobbyist.

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

Re: Cat Home DIY Q&A - Solenoid MCP23017 How To

Fri Jan 25, 2019 4:10 am

tlfong01 wrote:
Thu Jan 24, 2019 2:56 pm

PCF9591 8-bit AD/DA Module with Photo Sensor and Thermal Sensor - ¥9

When checking out the triple I2C TSX0104 3V to 5V logical level converter board, I happily found on board a little guy I forgot - the PCF8591 module, with photo and thermal sensor.

Now I can use this cheap thing for my poorman's water feature - when dark, switch on fountain, and when hot, switch on more cooling water jets, etc ...

...
Attachments
pcf8591_ad_da_2019jan2501.jpg
pcf8591_ad_da_2019jan2501.jpg (183.72 KiB) Viewed 2749 times
I am an electronics and smart home hobbyist.

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

Re: Cat Home DIY Q&A - Solenoid MCP23017 How To

Fri Jan 25, 2019 7:40 am

tlfong01 wrote:
Fri Jan 25, 2019 4:10 am
PCF9591 8-bit AD/DA Module ]

Ping PCF8591 Function OK

I have written a function to ping PCF8591 and found everything OK. The ping program can now ping 3 devices: DS3231, MCP23017, and PCF8591.

...

Code: Select all

# pingI2cDev167 tlfong01 2019jan25hkt1528

# Rpi3B+ Raspbian stretch linux 4.14.34-v7+ arm python3.5.3

# Program Description
# Ping DS3231 and MCP23017 by writing to a register and then reading back
# Ping PCF8591 by DAC 0x7f 

import smbus
i2cCh = smbus.SMBus(1)

# DS3231 I2C and Register Addresses

ds3231DevAddrByte = 0x68 # DS3231 I2C Device Address
ds3231RegAddrByte = 0x03 # DS3231 Day Register (only lower nibble can write/read)

# MCP23017 I2C and Register Addresses
mcp23017DevAddrByte= 0x20 # MCP23017 I2C Device Address
mcp23017RegAddrByte= 0x00 # MCP23017 IO Direction Register Address

# PCF8591 I2C and Register Addresses
pcf8591DevAddrByte  = 0x48 # PCF8591 I2C Device Address
pcf8591ControlByte1 = 0x40 # PCF8591 Control Byte1 (Analog Output Enable)
pcf8591ControlByte2 = 0x7f # PCF8591 Control Byte2 (Convert 1/2 Vcc = 2.5V)

# *** Read Write Print Device/Register Functions ***

def writeDevTwoBytes(i2cPort, devAddrByte, dataByte1, dataByte2):
    i2cPort.write_byte_data(devAddrByte, dataByte1, dataByte2)
    return

def readDevRegOneByte(i2cPort, devAddrByte, regAddrByte):
    readByte = i2cPort.read_byte_data(devAddrByte, regAddrByte)
    return readByte

def writeDevRegOneByte(i2cPort, devAddrByte, regAddrByte, writeByte):
    writeDevTwoBytes(i2cPort, devAddrByte, regAddrByte, writeByte)
    return

def printDevRegOneByte(i2cPort, devAddrByte, regAddrByte, printTitle):
    readByte = readDevRegOneByte(i2cPort, devAddrByte, regAddrByte)
    print(printTitle, hex(readByte))

# *** Test Functions ***

def pingDs3231(testByte):
    devAddrByte = ds3231DevAddrByte
    regAddrByte = ds3231RegAddrByte
    printDevRegOneByte(i2cCh, devAddrByte, regAddrByte, 'Byte Read    =')
    writeDevRegOneByte(i2cCh, devAddrByte, regAddrByte, testByte)
    print('Byte Written =', hex(testByte))
    printDevRegOneByte(i2cCh, devAddrByte, regAddrByte, 'Byte Read    =')

def pingMcp23017(testByte):
    devAddrByte = mcp23017DevAddrByte
    regAddrByte = mcp23017RegAddrByte
    printDevRegOneByte(i2cCh, devAddrByte, regAddrByte, 'Byte Read    =')
    writeDevRegOneByte(i2cCh, devAddrByte, regAddrByte, testByte)
    print('Byte Written =', hex(testByte))
    printDevRegOneByte(i2cCh, devAddrByte, regAddrByte, 'Byte Read    =')

def pingPcf8591(testControlByte):
    devAddrByte = pcf8591DevAddrByte
    controlByte1 = pcf8591ControlByte1
    writeDevTwoBytes(i2cCh, devAddrByte, controlByte1, testControlByte)
    return
    
def testPingDs3231():
    print('\nPing DS3231 Twice, ...')
    print('Ping #1')
    pingDs3231(0x5)
    print('Ping #2')
    pingDs3231(0x7)    
    return

def testPingMcp23017():
    print('\nPing Mcp23017 Twice, ...')
    print('Ping #1')
    pingMcp23017(0x66)
    print('Ping #2')
    pingMcp23017(0x88)    
    return

def testPingPcf8591():
    print('\nConvert Pcf8591 1/2 Vcc, ...')
    print('Convert #1')
    pingPcf8591(0x7f)
    print('Convert #2')
    pingPcf8591(0x7f)    
    return

# *** Main ***

#testPingDs3231()
#testPingMcp23017()
testPingPcf8591()

# *** Sample Output ***

'''
Ping DS3231 Twice, ...
Ping #1
Byte Read    = 0x7
Byte Written = 0x5
Byte Read    = 0x5
Ping #2
Byte Read    = 0x5
Byte Written = 0x7
Byte Read    = 0x7

Ping Mcp23017 Twice, ...
Ping #1
Byte Read    = 0x88
Byte Written = 0x66
Byte Read    = 0x66
Ping #2
Byte Read    = 0x66
Byte Written = 0x88
Byte Read    = 0x88
'''
'''
Convert Pcf8591 1/2 Vcc, ...
Convert #1
Convert #2
'''

# *** End ***
I am an electronics and smart home hobbyist.

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

Re: Cat Home DIY Q&A - Solenoid MCP23017 How To

Fri Jan 25, 2019 11:54 am

tlfong01 wrote:
Fri Jan 25, 2019 4:10 am
When checking out the triple I2C TSX0104 3V to 5V logical level converter board, I happily found on board a little guy I forgot - the PCF8591 module, with photo and thermal sensor.

Too Many I2C Pullups Overloading TSX0104

I can ping PCF8591 alone without any problems. But if I put 3 devices (M017, DS3231, and PCF8691) on, then i2cdetect has intermittent no detect problems. I guess too many 4k7 resistors are overloading TSX0104. So I need to think if I should remove all the pullups except 1.

Amazon MCP23017-E/SS I2C Interface 16 Channel IO Expansion Module Compatible Arduino C51 IIC Input and Output Expansion Board - $17
https://www.amazon.com/WHDTS-MCP23017-i ... B07GRQWQF9

TaoBao MCP23017 I2C Interface 16 Channel IO Expansion Module ¥13
https://item.taobao.com/item.htm?spm=a1 ... 3164823777

* strong driving capacity 25mA.
* supports 100KHz, 400KHz and 1.7MHz.
* 3 address pins for 8 different addresses = 8 modules 128 GPIO pins.


mcp23017 Datasheet
https://images-na.ssl-images-amazon.com ... 1sQTmL.pdf

User Guide
https://images-na.ssl-images-amazon.com ... N7I7WL.pdf
...
Attachments
m017_pullup_add_jumper_2019jan2501.jpg
m017_pullup_add_jumper_2019jan2501.jpg (159.94 KiB) Viewed 2715 times
I am an electronics and smart home hobbyist.

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

Re: Cat Home DIY Q&A - Solenoid MCP23017 How To

Fri Jan 25, 2019 2:23 pm

tlfong01 wrote:
Fri Jan 25, 2019 11:54 am

MCP23017 Quartet

So I completed the boring job of crimping DuPont connector cables by hand and wiring to 4 MCP23017 modules, and to 3 I2C Mux cables.

When doing boring jobs, I usually listen to the radio. Just now I checked the BBC in Our Time programme and had a very pleasant surprise - they were talking about Emmy Noether, whose Theorem I have been reading this couple of days when solving problems on what size of fuses to use, and how the velocity of water from 1/2" to 1/4" pipe changes. It is all about invariants or conservation of energy [1], [2], ...

Anyway, I used i2cdetect -y 1 to detect all 4 MCP23017 modules OK.

Next step is to switch 64 solenoids! :mrgreen:

[1] Norton and Thevenin Theorems
https://www.raspberrypi.org/forums/view ... 5#p1412494

[2] Noether Theorem
https://www.raspberrypi.org/forums/view ... 5#p1413515

Emmy Noether - BBC Radio 4 In Our Time 2019jan24
https://www.bbc.co.uk/programmes/m00025bw

Dvorak:String Quartet op.51
https://www.youtube.com/watch?time_cont ... mLMKYcncto

Update 2019jan27hkt0919

RE: CAT HOME DIY QUESTION - HOW TO USE SOLENOID VALVE Postby tlfong01 » 2019-Jan-21 Mon 12:03 pm
https://www.raspberrypi.org/forums/view ... 4#p1419324

Bernoulli's principle - Wikipedia
https://en.wikipedia.org/wiki/Bernoulli%27s_principle

In fluid dynamics, Bernoulli's principle states that an increase in the speed of a fluid occurs simultaneously with a decrease in pressure or a decrease in the fluid's potential energy. The principle is named after Daniel Bernoulli who published it in his book Hydrodynamica in 1738.

Bernoulli's principle can be derived from the principle of conservation of energy. This states that, in a steady flow, the sum of all forms of energy in a fluid along a streamline is the same at all points on that streamline. This requires that the sum of kinetic energy, potential energy and internal energy remains constant. Thus an increase in the speed of the fluid – implying an increase in its kinetic energy (dynamic pressure) – occurs with a simultaneous decrease in (the sum of) its potential energy (including the static pressure) and internal energy. If the fluid is flowing out of a reservoir, the sum of all forms of energy is the same on all streamlines because in a reservoir the energy per unit volume (the sum of pressure and gravitational potential) is the same everywhere, ...

...
Attachments
quad_mcp23017_2019jan1501.jpg
quad_mcp23017_2019jan1501.jpg (194.13 KiB) Viewed 2701 times
Last edited by tlfong01 on Sun Jan 27, 2019 1:25 am, edited 2 times in total.
I am an electronics and smart home hobbyist.

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

Re: Cat Home DIY Q&A - Solenoid MCP23017 How To

Sat Jan 26, 2019 4:30 am

tlfong01 wrote:
Fri Jan 25, 2019 2:23 pm
MCP23017 Quartet
I used i2cdetect -y 1 to detect all 4 MCP23017 modules OK.
Next step is to switch 64 solenoids!

MCP23017 Octet Assembly and Testing Notes

On second thought, because I can stack more M017 modules on the same board, I can be a bit greedy and place 8 modules on board, to make a M017 octet, then:

I can switch 16 x 8 = 128 water jets at the same time! :mrgreen:

So, more boring and tedious SMD soldering work today, but never tomorrow, Sunday, 'cause that's the day I rest!

Never On Sunday - The Chordettes 1,460,762 views
https://www.youtube.com/watch?v=p57nf-FvVxw

...
Attachments
m017_pullup_addr_jumper_solder_job_2019jan2601.jpg
m017_pullup_addr_jumper_solder_job_2019jan2601.jpg (125.32 KiB) Viewed 2670 times
I am an electronics and smart home hobbyist.

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

Re: Cat Home DIY Q&A - Solenoid MCP23017 How To

Sat Jan 26, 2019 7:09 am

tlfong01 wrote:
Sat Jan 26, 2019 4:30 am

MCP23017 Driving GM3B202P SSR (Sold State Relay)

Soldering MCP23017 SMD work is too boring. So I am taking a break to do something more interesting.

I searched my junk components box and found a couple of SSRs. I am thinking of using them for the water feature thing, because I don't have enough EMRs (Electro Magnetic Relay) to go around.


I googled and found some seemingly useful spec for the SSR. And as usual, the English is too geeky to read. I need to experiment to check out if I correctly understand what is going on.

AliExpress 5V 1 Channel OMRON SSR G3MB-202P Solid State Relay Module 240V 2A Output with Resistive Fuse For Arduino M63 US $1.38
https://www.aliexpress.com/item/5V-1-Ch ... 0028.html

Description
1. Omron 5V Solid State Relays 240V 2A
2. Resistive insurance Wire 240V 2A
3. Size: LWH 25 * 34 * 25
4. Weight: 13g
5. Input power: 5V DC
6. Input Current: 160MA
7. Control signal: 0 ~ 1.5V Low state relay ON, 2.5-5V High state relay OFF
8. Connector: Blue KF301


Input Connector
1. DC+ : Positive of relay power supply
2. DC- : Negative of relay power supply
3. CH : Low Trigger


Electrical Spec
Voltage = 5V
Quiescent Current = 0mA
Working current = 12.5mA
Trigger voltage = 0 ~ 1.5V
Trigger current = 2mA

...
Attachments
gm3b202p_ssr_2019jan2601.jpg
gm3b202p_ssr_2019jan2601.jpg (157.55 KiB) Viewed 2655 times
I am an electronics and smart home hobbyist.

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

Re: Cat Home DIY Q&A - Solenoid MCP23017 How To

Sat Jan 26, 2019 8:04 am

tlfong01 wrote:
Sat Jan 26, 2019 7:09 am
MCP23017 Driving GM3B202P SSR (Sold State Relay)
AliExpress 5V 1 Channel OMRON SSR G3MB-202P Solid State Relay Module 240V 2A Output with Resistive Fuse For Arduino M63 US $1.38
https://www.aliexpress.com/item/5V-1-Ch ... 0028.html
1. Omron 5V Solid State Relays 240V 2A
5. Input power: 5V DC
6. Input Current: 160MA
7. Control signal: 0 ~ 1.5V Low state relay ON, 2.5-5V High state relay OFF
Electrical Spec
Voltage = 5V
Quiescent Current = 0mA
Working current = 12.5mA
Trigger voltage = 0 ~ 1.5V
Trigger current = 2mA

Omron G3MB202P SSR In vs Vin Test Results

The test results in displayed below. I found it High level trigger, with trigger voltage around 1.4V. There is no hysteresis.

I need to google the Omron SSR spec to confirm if I miss anything. There is one thing I did not understand, that is the fuse thing, which is not seen in KY019 EMR (Electrical Magnetic Relay).

...
Attachments
ssr_test_2019jan2601.jpg
ssr_test_2019jan2601.jpg (138.56 KiB) Viewed 2651 times
I am an electronics and smart home hobbyist.

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

Re: Cat Home DIY Q&A - Solenoid MCP23017 How To

Sat Jan 26, 2019 8:42 am

tlfong01 wrote:
Sat Jan 26, 2019 8:04 am
Omron G3MB202P SSR In vs Vin Test Results[/b]
I found it High level trigger, with trigger voltage around 1.4V. There is no hysteresis.
I need to google the Omron SSR spec to confirm if I miss anything. There is one thing I did not understand, that is the fuse thing, which is not seen in KY019 EMR (Electrical Magnetic Relay)
.


Omron G3MB202P Spec Summary

I read the datasheet and found it quite different from electromagntic relays. For low voltage, say 24VDC/AC or less, current less than 2A, I think it is a good choice.

There are 2 things I still don't understand:

1. Is is OK to use for DC switching, say 12~24VDC, 2A?

2. What is the input resistor, and the fuse?


Update 2019jan26hkt1653

I read the following SSR Common Precautions by Omron and learnt that there are too many precautions I need to consider, even I am using it for low DC voltages.

So on second thought, I decided not to use for my current projects.


Solid State Relays Common Precautions - Omron
https://omronfs.omron.com/en_US/ecb/pro ... ns_ssr.pdf
...
Attachments
org3mb5v_spec_2019jan2601.jpg
org3mb5v_spec_2019jan2601.jpg (192.88 KiB) Viewed 2643 times
I am an electronics and smart home hobbyist.

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

Re: Cat Home DIY Q&A - Solenoid MCP23017 How To

Sat Jan 26, 2019 1:17 pm

tlfong01 wrote:
Sat Jan 26, 2019 8:42 am
I read the following SSR Common Precautions by Omron and learnt that there are too many precautions I need to consider, even I am using it for low DC voltages.
Solid State Relays Common Precautions - Omron

https://omronfs.omron.com/en_US/ecb/pro ... ns_ssr.pdf

Technical Explanation for Solid-state Relays - Omron

To learn more about SSR, I read more references, including the following very good one by Omron.

Technical Explanation for Solid-state Relays - Omron
https://www.ia.omron.com/data_pdf/guide ... _e_9_2.pdf

I found SSR is much more complicated than I thought. I tried to use the Omron SSR to replace the EMR KY019, but it does not seem to work. Then I googled to read the following:

Quora Q&A - Can I use DC voltage at the output in a solid state relay that is specified for an AC output?
https://www.quora.com/Can-I-use-DC-volt ... -AC-output

No. Because solid-state relay, triac or antiparallel SCRs commutate (becomes off) at the zero crossing of the AC when there is no trigger. If you use DC, then, once it becomes on, it will remain on, even if you want to turn it off because there is no natural commutation.

EE Stack Exchange
https://electronics.stackexchange.com/q ... dc-current

It depends on the relay. Many solid state relays use a thyristor as their main switching component, and a thyristor can't be turned off simply by removing the trigger voltage. They rely on the reverse-bias portion of the AC cycle to switch off. Other solid state relays use FETs as their switching component and those relays may work with DC. It all depends on what the specific relay is rated for, and the only way to know is to carefully read the relay's data sheet.

My Conclusion

I think that SSR is too complicated for a AC power circuit newbie like me. I am too scared to study the power devices triac and thyrsitor. So I decided to give up, put the SSR back to the junk components box, and return to the very boring MCP23017 wiring work.
Last edited by tlfong01 on Sat Jan 26, 2019 2:55 pm, edited 2 times in total.
I am an electronics and smart home hobbyist.

User avatar
rpdom
Posts: 18871
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Cat Home DIY Q&A - IOX MCP23017 How To

Sat Jan 26, 2019 1:23 pm

Solid State Relays are no use for switching DC voltage. They use a triac, which once will not turn off once triggered as long as there is constant current flowing through it. AC mains is not constant. It switches on and off 100 to 120 times a second, depending on your country.

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

Re: Cat Home DIY Q&A - IOX MCP23017 How To

Sat Jan 26, 2019 1:56 pm

rpdom wrote:
Sat Jan 26, 2019 1:23 pm
Solid State Relays are no use for switching DC voltage. They use a triac, which once will not turn off once triggered as long as there is constant current flowing through it. AC mains is not constant. It switches on and off 100 to 120 times a second, depending on your country.

SSRs are no use for switching DC Voltage

Many thanks for your confirmation. Earlier I already assembled and tested OK some electromagnetic / mechanic relays switching AC24V and AC220V. I found everything straightforward. So I will keep it simple - Mechanical relays for ever!

One thing I found SSR uncomfortable to use is that they don't have the ensuring click click sound when switching on/off.

Anyway, I have already kissed good bye to SSR.


Kiss Me Good Bye - Petula Clark 2,140,094 views
https://www.youtube.com/watch?v=-wulPDIG9Zs

Time to say goobye - Andrea Bocelli I Sarah Brightman - 49,837,405 views
https://www.youtube.com/watch?v=KPrf-svWUAQ
I am an electronics and smart home hobbyist.

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

Re: Cat Home DIY Q&A - IOX MCP23017 How To

Sun Jan 27, 2019 2:06 am

tlfong01 wrote:
Sat Jan 26, 2019 1:56 pm
rpdom wrote:
Sat Jan 26, 2019 1:23 pm
Solid State Relays are no use for switching DC voltage, ...
Anyway, I have already kissed good bye to SSR, ...

AdaFruit Youtube Tutorial on WiFi AC/DC SSR

This morning I googled and found AdaFruit does has a SSR that can switch both AC and DC. Their YT tutorials is really good and clarifies my mind. :mrgreen:

AdaFruit ESP8266 MQTT AC/DC SSR Youtube
https://www.youtube.com/watch?time_cont ... 2rQurTNd38

Adafruit Latching Mini Relay FeatherWing (AC or DC Power) $8
https://www.adafruit.com/product/2923

Can turn on and off lamps, fans, solenoids, and other small appliances that run on up to 250VAC or DC power, ...

Can switch up to 2A of resistive current at 30VDC or ~40VAC or lower, ...


AdaFruit Support Forum - SSR vs Relay Moderators: adafruit_support_bill, adafruit
https://forums.adafruit.com/viewtopic.php?f=25&p=264853

AdaFruit Art Controller Relay Board Kit $18
https://www.adafruit.com/product/1038

Sharp S108T02 Zero Cross type Triac output SSR Datasheet
https://cdn.sparkfun.com/datasheets/Com ... Series.pdf

SparkFun Solid State Relay Kit (AC Only) - Retired
https://www.sparkfun.com/products/retired/10684

This product has been retired from our catalog and is no longer for sale. This page is made available for those looking for datasheets, ...

A solid state relay (SSR) is just what it sounds like; an IC that acts like a mechanical relay. They allow you to control high-voltage AC loads from lower voltage DC control circuitry. Solid state relays, however, have several advantages over mechanical relays. One such advantage is that they can be switched by a much lower voltage and at a much lower current than most mechanical relays. Also, because there’s no moving contacts, solid state relays can be switched much faster and for much longer periods without wearing out.

This kit has everything you need to make use of an SSR in your next high-voltage project. After soldering together the through-hole kit, simply attach your AC load and switch it on and off using your microcontroller. This is a great kit for through-hole soldering practice and is really handy for controlling lights, water pumps, fans or any other intermittent high-voltage device.

Note: These work with AC loads only. Great care, safety, and knowledge should be used with high current and voltage.
I am an electronics and smart home hobbyist.

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

Re: Cat Home DIY Q&A - IOX MCP23017 How To

Sun Jan 27, 2019 3:57 am

tlfong01 wrote:
Sat Jan 26, 2019 1:56 pm
rpdom wrote:
Sat Jan 26, 2019 1:23 pm
Solid State Relays are no use for switching DC voltage, ...
So newbie me will keep it simple - mechanical relays forever!, ...

Panasonic APAN3124 1a5A x 8 Mechanical Power Relay Module

Just now I googled again and found the following relay x 8 module good. I am thinking of trying them later.

Chentron Panasonic APAN3124 1a5A NPN/PNP RT-P Mechanical Power Relay Module CN¥106
https://item.taobao.com/item.htm?spm=20 ... 8345955555

Chentron Power Relays
https://oyunele.world.taobao.com/?spm=2 ... 71c16rHclN

Panasonic APAN3124 Power Relay Datasheet
https://www.panasonic-electric-works.co ... a_n_en.pdf
...
Attachments
mechanical_power_relay_module_2019jan2701.jpg
mechanical_power_relay_module_2019jan2701.jpg (129.59 KiB) Viewed 2563 times
Last edited by tlfong01 on Sun Jan 27, 2019 5:34 am, edited 2 times in total.
I am an electronics and smart home hobbyist.

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

Re: Cat Home DIY Q&A - IOX MCP23017 How To

Sun Jan 27, 2019 4:51 am

tlfong01 wrote:
Sun Jan 27, 2019 3:57 am
Panasonic APAN3124 1a5A x 8 Power Realy Module
Chentron Panasonic APAN3124 1a5A NPN/PNP RT-P Power Realy Module CN¥106
https://item.taobao.com/item.htm?spm=20 ... 8345955555
Chentron Power Relays
https://oyunele.world.taobao.com/?spm=2 ... 71c16rHclN
Panasonic APAN3124 Power Relay Datasheet
https://www.panasonic-electric-works.co ... a_n_en.pdf
...

Water Feature Circuit Design Notes v0.2

Now I have modified the old design to V0.2, using DigiKey's relay terminology

Definition of Relay Terminology - DigiKey
https://media.digikey.com/pdf/Other%20R ... 20Info.pdf
...
Attachments
water_feature_ciccuit_2019jan2702.jpg
water_feature_ciccuit_2019jan2702.jpg (190.6 KiB) Viewed 2558 times
I am an electronics and smart home hobbyist.

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

Re: Cat Home DIY Q&A - Solenoid MCP23017 How To

Sun Jan 27, 2019 3:12 pm

tlfong01 wrote:
Sat Jan 26, 2019 7:09 am
Soldering MCP23017 SMD work is too boring. So I am taking a break to do something more interesting.
I searched my junk components box and found a couple of SSRs. I need to experiment to check out if I correctly understand what is going on.

MCP23017 Octet Wiring Completed, Each One of Eight Modules Detectable by $ i2cdetect -y 1
...
Attachments
mcp23017_octet_2019jan2701.jpg
mcp23017_octet_2019jan2701.jpg (224.44 KiB) Viewed 2524 times
I am an electronics and smart home hobbyist.

Return to “Automation, sensing and robotics”