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

Re: Relay Module KY-019 5V

Wed Jul 25, 2018 8:27 am

tlfong01 wrote:
Tue Jul 24, 2018 5:44 am
My high class, ¥9.50, digital, constant current, multi (24) smoothly diffusive light sources, 'absolutely' no flicker, 6500K, 12W, LED light source module mounted on a bracket, ... This AC220V light source module is to be controlled by the KY109 relay box, along with the Permanent Magnet Synchronous Motor Clock.

I am thinking of using a DC12V cpu fan to cool down the LED module. The fan will be controlled by KY019 relay. The rotation signal will be connected to a MCP23017 interrupt pin input pin, so I can adjust the fan speed to control the LED module temperature. This reminds me that the DS3231 Real Time Clock actually has a built in temperature sensor, so I can make use of it, instead of using another temperature sensor and or an ADC.

I am preparing to set up my second RpiZW. I bought a mini HDMI to standard HDMI adapter for 15 yuans. Last time I watched Big Clive tearing down things and found him using a magnifier like mine. So now I know my little magnifier bought from the flea market is a professional tool to be proud of. I also noticed that Big Clive is using a Made in China multimeter, seemingly cheaper than mine.

Appendix - 12VDC 0.3A Cpu Fan NMB 2806GL-04W-B59 spec

NMB 2806GL-04W-B59 12V 0.3A 3wires 7cm Cpu Cooling Fan ¥18.00
Manufacturer NMB
Part number 2806GL-04W-B59
AC/DC DC Fans
Fan Type Axial
Size 70 mm x 70 mm x 15 mm
Voltage 12V
Current 0.3A
Termination 3 wires

bearing = double
speed measurement = yes
leaf number = 9
wire length = 18.5CM
speed full speed = 4200rpm
capqacity full speed = 35.0 cfm
approx noise full speed = 36.5 dBA
Attachments
zDyIAiE[1].jpg
zDyIAiE[1].jpg (73.11 KiB) Viewed 1531 times
Last edited by tlfong01 on Wed Jul 25, 2018 2:05 pm, edited 2 times in total.
I am an electronics and smart home hobbyist.

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

Re: Relay Module KY-019 5V

Wed Jul 25, 2018 8:31 am

tlfong01 wrote:
Mon Jul 23, 2018 7:49 am
Mortimer wrote:
Thu Jul 05, 2018 9:31 am
  • ... turn any switches to on position on the project, then stand back and plug into a switched socket and turn on.
  • Once turned on, if you intend measuring for Voltages .use one hand only, the other kept well away and behind your back

I am wiring the high class 220VAC LED lamp for my studio. Your advice of putting one hand behind my back is not practical. The mains terminal connector is floating in the air. How can I use one hand to hold the thing, and use the same hand to hold a screw driver?
When working with mains working safely should be paramount. I think the problem you had was not to make working with one hand practical. With exposed mains the connector should have been secured, so that you could work safely in the knowledge that connector with dangerous Voltages was not going to move in an unexpected way.

The idea is to make sure you do not inadvertently put yourself in a situation where current can flow from one hand to the other across your heart.

Alternative measure: Wear rubber gloves, even on just one hand if good manual dexterity is required.

tlfong01 wrote:
Mon Jul 23, 2018 7:49 am

BTW, my lamp does not flicker. Big Clive in a youtube tore down a similar lamp (I now know that he should be world famous for tearing down things) and explained the circuit inside out, even showed the datasheet of the power electronic IC and circuit, and pointed out which resistor to modify, to adjust the brightness of the lamp. As usual, I learnt many news things that I didn't know that I didn't know,including the following:
Good isn't he, I watch far too much "Big Clive" (and Julian Ilett and John Ward if I'm honest!!).


Lastly, it looks like you're having way too much fun!! :mrgreen:
--------------
The purpose of a little toe is to ensure you keep your furniture in the right place.

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

Re: Relay Module KY-019 5V

Wed Jul 25, 2018 8:33 am

tlfong01 wrote:
Sun Jul 22, 2018 5:00 am
Mortimer wrote:
Sat Jul 21, 2018 4:52 pm
You could try this...
https://www.codecademy.com

Codecademy seems good if you wish to learn many programming languages in a short time and becomes a pro. But to become a codecademy pro, you need to pay up.
Apologies for that, when I used Codecademy it was some time ago and I didn't need to pay for anything. It must have changed. I'll take a look sometime and see what it is like now, so I have a better idea of its current status. :geek:
--------------
The purpose of a little toe is to ensure you keep your furniture in the right place.

User avatar
davidcoton
Posts: 4258
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: Relay Module KY-019 5V

Wed Jul 25, 2018 8:44 am

Mortimer wrote:
Wed Jul 25, 2018 8:33 am
tlfong01 wrote:
Sun Jul 22, 2018 5:00 am
Mortimer wrote:
Sat Jul 21, 2018 4:52 pm
You could try this...
https://www.codecademy.com

Codecademy seems good if you wish to learn many programming languages in a short time and becomes a pro. But to become a codecademy pro, you need to pay up.
Apologies for that, when I used Codecademy it was some time ago and I didn't need to pay for anything. It must have changed. I'll take a look sometime and see what it is like now, so I have a better idea of its current status. :geek:
Yes, Codeacademy has changed, and from my POV not for the better. I can no longer recommend them, as I used to do.
Signature retired

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

Re: Relay Module KY-019 5V

Wed Jul 25, 2018 8:59 am

tlfong01 wrote:
Tue Jul 24, 2018 8:06 am
So I redrew the schematic, ... include EL817C and ULN2803, ...

Now I have also added two more MCP23017 to the RpiZW I2C bus. The terminal command

i2c-detect -y 1 detects addresses 0x21, 0x22, and 0x23.
Attachments
d6gK2Ox[1].jpg
d6gK2Ox[1].jpg (100.25 KiB) Viewed 1518 times
I am an electronics and smart home hobbyist.

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

Re: Relay Module KY-019 5V

Wed Jul 25, 2018 9:22 am

davidcoton wrote:
Wed Jul 25, 2018 8:44 am

Yes, Codeacademy has changed, and from my POV not for the better. I can no longer recommend them, as I used to do.
That's a shame, it was brilliant when I used it. I suppose it shows how quickly things change and how it is not always in a good direction.

Having logged back in I see the 'free' courses are still accessible, but it isn't at all clear how to get to them as a new user. It may be because I've already done them, but it looks to me like it should be possible.

Go into Codecademy and log in.
Click "Catalogue" at the top.
under "By Language" click "Python".

I then see three sections "Intensive Programs", "Courses" and "You might also be interested in".
The 'free' option for Python appears under "Courses" for me.
The "Intensive Programs" are the "Pro" courses.
--------------
The purpose of a little toe is to ensure you keep your furniture in the right place.

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

Re: Relay Module KY-019 5V

Wed Jul 25, 2018 9:24 am

Mortimer wrote:
Wed Jul 25, 2018 9:22 am
davidcoton wrote:
Wed Jul 25, 2018 8:44 am

Yes, Codeacademy has changed, and from my POV not for the better. I can no longer recommend them, as I used to do.
That's a shame, it was brilliant when I used it. I suppose it shows how quickly things change and how it is not always in a good direction.

Having logged back in I see the 'free' courses are still accessible, but it isn't at all clear how to get to them as a new user. It may be because I've already done them, but it looks to me like it should be possible.

Go into Codecademy and log in.
Click "Catalogue" at the top.
under "By Language" click "Python".

I then see three sections "Intensive Programs", "Courses" and "You might also be interested in".
The 'free' option for Python appears under "Courses" for me.
The "Intensive Programs" are the "Pro" courses.
Actually scratch that, I have just taken a closer look at my 100% Python course completion record. About 50% of the content I can no longer see without paying for a Pro upgrade, so yes, Codecademy is a no go now. What a pity.
--------------
The purpose of a little toe is to ensure you keep your furniture in the right place.

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

Re: Relay Module KY-019 5V

Wed Jul 25, 2018 1:27 pm

Mortimer wrote:
Wed Jul 25, 2018 8:33 am
tlfong01 wrote:
Sun Jul 22, 2018 5:00 am
Codecademy seems good if you wish to learn many programming languages in a short time and becomes a pro. But to become a codecademy pro, you need to pay up.
Apologies for that, when I used Codecademy it was some time ago and I didn't need to pay for anything. It must have changed. I'll take a look sometime and see what it is like now, so I have a better idea of its current status. :geek:

No problem at all. Last time I mentioned that earlier I went back to the MIT course I took some years ago, but I could no longer find the stuff I wish to revise, perhaps they have moved to somewhere else. I did find the updated version of the course, using pathon 3.5 instead of 2.7. But the web page of the updated course says it is not available, and the course is still free but under edX, in cooperation with another college, and that if you wish to gain credit from that college, you need to pay a fee. I think the new version of MIT OCW disappoints me as the new Codecademy disappoints you .

Errata - The new Python 3.5 course was not available when I visited because it was between terms. It is available now. My apologies for the wrong information. I would recommend the new course now. :D

I read the following reviews and not sure now. I am not sure if one can watch the video WITHOUT paying any fee.

Reviews for Charter Oak State College
https://www.yelp.com/biz/charter-oak-st ... ew-britain


MIT OCW / edX New Python 3.5 Course (Free, and USD49 for a verified cert)
https://www.edx.org/course/introduction ... ing-python

I once found the free 2008 MIT course good, but I won't recommend the same old course to my friends not because it is old, but it is very hard (MIT hard!). I did struggle for about 50% of the lectures before dropping out. I checked the youtube viewing statistics and found that Lect 1 had 4 million viewers, but only 80k left in the last lecture. In other words, only 2% completed (viewing) the whole course. In the beginning I thought I would work hard to complete the course and achieve MIT standard, perhaps as the Codecademy starters hoping to become a pro after the course. I think for Codecademy a percentage much higher than 2% work very hard, pay the fee, complete the course, get a certificate, and enter the profession, and begin a very long journey to become a pro.

Lec 1 | MIT 6.00 Introduction to Computer Science and Programming, Fall 2008 4,133,358 views
https://www.youtube.com/watch?v=k6U-i4gXkLM

Lec 24 | MIT 6.00 Introduction to Computer Science and Programming, Fall 2008 83,195 views
https://www.youtube.com/watch?v=2q--tAPkVXI [83/4133] == 2%

When I was small, I often complained this and that, like why others have a rich dad, me a poor dad. My grandmother then told me 'there ain't such thing as free lunch'. And never mind rich dad poor dad, because (she then sang me a song, which after so many years, I still remember the first verse):

Every man has a happy family. Every home has a hard prayer to say.

I have a happy family
https://www.youtube.com/watch?v=dGT3b2or6c8

It was many years later that I learnt that what my grandmother hoped me to understand is the Anna Karenina principle which says:

Happy families are all alike; every unhappy family is unhappy in its own way.

I learnt the Anna Karenina principle in a predicate logic class when the teacher was teaching declarative programming using Prolog.

A similar thing is the Black Swan Theory which is very important and useful in everyday life (bare hand bare foot fixing AC200V LED lamp, say) or database apps, or selecting methodology or software package in business information systems analysis and design (ISAD).

Of course nowadays everybody goes Agile, and nobody heard about Prolog, but I think what my grandmother taught me long long time ago makes my long long journey to become a pro a little bit shorter.
Attachments
edx_python_2018jul2801.jpg
edx_python_2018jul2801.jpg (140.95 KiB) Viewed 1299 times
Last edited by tlfong01 on Sat Jul 28, 2018 2:37 pm, edited 10 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: Relay Module KY-019 5V

Wed Jul 25, 2018 3:08 pm

tlfong01 wrote:
Wed Jul 25, 2018 7:49 am
This morning I shortened all the long names and the program now looks smart, as after a clean hair cut. I also walked through the program quickly and refactored things. Version 1.4 is listed below. I found more typo and format errors and would correct to Vision 1.5.

I have changed my mind not to update IOX (IO Expander) mcp23017 MCVNE to V1.5. Instead I write a MCVNE v1.5 for the RTC (Real Time Clock) DS3231.

The RTC MCVNE V1.5 uses the same I2C read/write device register functions in IOX MCVNE 1.4. Of course I can import the IOX functions to RTC MCVNE, but then RTC MCVNE is not complete. So I just copy the read/write/print functions (also the setup/debug notes) from IOX MCVNE to RTC MCVNE.

RTC MCVNE is very similar to IOX MCVNE, except the device address and device register address.

Now that I have no problem reading and writing a data byte to IOX or RTC devices, then I will have little problem configuring both devices to control anything, because all control is done by reading and writing registers. The programming is very tedious, but it is only register read and write, and it is not that difficult to google Arduino program examples and borrow or steal something for RpiZW. To morrow I will write simple operations such as reading RTC temperature, setting IOX output pin to High or Low etc.

Here is debugged, no problem, RTC MCVNE V1.5 (about 100 lines long. I number it 1.5 and not from 1.0, because it uses things from RTC MCVNE 1.4). It might take you one or two hours reading the complicated data sheet (read only the relevant paragraphs, not the whole date sheet, which might take you days to fully understand. Remember to eat the big elephant one bite at a time!)

Code: Select all

#!/usr/bin/python3

# KY019 DS3231 Real Time Clock Test Program V1.5 tlfong01 2018jul25hkt2230

# *** Short Description ***
# 1. read, write, print Day Register.

# *** Long Description ***
# 1. Read and print the old byte already in Day Register 
# 2. Write a new byte to the register.
# 3. Read back the new byte. 
# 4. Print the new byte.
# 5. Do it one more time.

# *** Setup/Debug Notes ***
# 1. To change I2c speed to 100Khz
#    $ sudo nano /boot/config.txt
#    modify the dtparam=i2c_arm_on line as below 
#    dtparam=i2c_arm=on, i2c_arm_baudrate=100000
# 2. I2c speeds list
#    low speed      =  50kHz ( 50000) 
#    standard speed = 100KHz (100000)
#    maximum speed  = 400KHz (400000)
# 3. I2C speed can only be changed at raspbain boot time.  It cannot be changed using python.

# *** Import ***

import datetime
import smbus

# *** Config ***

# Raspberry Pi                              = Raspberry Pi Zero W V1.1
# Raspbian                                  = Linux 9 (stretch 2018 April)

programTitle                                = 'DS3231 RTC Example Test Program V1.5'
timeNowStr                                  = str(datetime.datetime.now())[0:16]
i2cCh1                                      = smbus.SMBus(1)

dataByte0x04                                = 0x04
dataByte0x06                                = 0x06

dvAddrByte                                  = 0x68

dayRegAddrByte                              = 0x03

# *** Device Functions (copied from MCP23008 test program example) ***

def readDvRegOneByte                    (i2cPort, dvAddrByte, DvRegAddrByte):
    readByte = i2cPort.read_byte_data   (         dvAddrByte, DvRegAddrByte)
    return readByte

def writeDvRegOneByte                   (i2cPort, dvAddrByte, DvRegAddrByte, writeByte):
    writeDviTwoBytes                    (i2cPort, dvAddrByte, DvRegAddrByte, writeByte)
    return

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

def printDvRegOneByte           (i2cPort, dvAddrByte, DvRegAddrByte, printTitle):
    readByte = readDvRegOneByte (i2cPort, dvAddrByte, DvRegAddrByte)
    print                       (printTitle, hex(readByte))

# *** Test Functions ***

def testWriteReadPrintDayReg(testDataByte):
    printDvRegOneByte(i2cCh1, dvAddrByte, dayRegAddrByte, 'Old Byte =')
    writeDvRegOneByte(i2cCh1, dvAddrByte, dayRegAddrByte, testDataByte)    
    printDvRegOneByte(i2cCh1, dvAddrByte, dayRegAddrByte, 'New Byte =')
    return

def test():
    print('*** TimeNow = ', timeNowStr, '***\n')
    print('***', programTitle, 'Begin ***\n')
    testWriteReadPrintDayReg(dataByte0x04)
    testWriteReadPrintDayReg(dataByte0x06)
    print('\n')
    print('***', programTitle, 'End  ***')
    return

# *** Main ***

test()

# *** Sample Output ***

'''
*** TimeNow =  2018-07-25 22:30 ***

*** DS3231 RTC Example Test Program V1.5 Begin ***

Old Byte = 0x6
New Byte = 0x4
Old Byte = 0x4
New Byte = 0x6


*** DS3231 RTC Example Test Program V1.5 End  ***
'''

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

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

Re: Relay Module KY-019 5V

Wed Jul 25, 2018 9:18 pm

With a DS3231 you're much better off using the built-in linux kernel driver which is activated by adding a simple line to /boot/config.txt

Code: Select all

dtoverlay=i2c-rtc,ds3231
Doing your own thing with a python program is a waste of time (excuse the pun).
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

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

Re: Relay Module KY-019 5V

Thu Jul 26, 2018 6:58 am

tlfong01 wrote:
Wed Jul 25, 2018 8:59 am
Now I have also added two more MCP23017 to the RpiZW I2C bus. The terminal command
i2c-detect -y 1 detects addresses 0x21, 0x22, and 0x23.

My 70cm x 40cm multi-function metal work/electrical work/electronic work/photo studio bench is getting overcrowded. So I have stacked up the 3 MCP23017 together, to squeeze more space for two new comers, a heater and a fan.

My test plan is something like below.

  • 1. RpiZW reads RTC's room temperature sensor every second.
  • 2. RpiZW tells IOX MCP23017 to set MCP23017's pins High or Low, which in turn controls KY019 AC220V 6A relays, connected to heater and fan.
  • 3. If room too cold, then switch on heater.
  • 4. If room too hot, then switch on fan.
  • 5. If room not too hot nor not too cold, switch on both.
Appendices

Whirlpool Ceramic Heater, HT015 (IP21), 1500W
https://www.whirlpool.com.hk/product/de ... er/ht015_1

Whirlpool's ceramic heaters provide warmth to keep you cozy throughout the winter. The ceramic heater produces an efficient heating source while maintaining a low surface temperature. It selectes the ideal power output level based on your settings and the existing temperature. Complying with IP21 test, HT015 has the drip protection feature.

HT015 - Ceramic Heater Series HK$400

1. IP21 Design
2. Detachable Dust Filter
3. Grounding Safety Device
3. Oscillation
5. Safety Thermostat
6. Tip Over Switch Safety Device


IMARFLEX 10H-E048 HK$148
http://www.my29544668.com/index.php?p=6&id=1526
Attachments
tjMIIUw[1].jpg
tjMIIUw[1].jpg (91.92 KiB) Viewed 1427 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: Relay Module KY-019 5V

Thu Jul 26, 2018 7:42 am

davidcoton wrote:
Tue Jul 24, 2018 3:42 pm
You must have spent too much time on the fake English fake news sites :lol:

Perhaps so. This morning the BBC alarm clock woke me up. I was half dreaming, but heard a Welsh guy talking about fake news. He said the English fake newspaper Daily Mirror fakely announced that Dunkirk was easy to win, and everybody happily shouted Winner Winner Chicken Dinner. The newsman was not pleased with so many fake English newspapers, so he crossed the ocean to edit non fake news.

Not sure if because lately I have been playing too much computer games, or liking too much FB, I vaguely heard the guy saying FB is the mother of all fakes, ...

BTW, the Coding for Free CA is inviting young dreamers to join the Millions Learning to Code with them. I think the ad is misleading because it doesn't tell the inconvenient truth of what percentage of the Millions actually completes their courses. So I think CA is not fake news, but fake ad.

Also BTW, is the American picture Dunkirk fake enough to worth watching?

Harold Evans at 90 - BBC Documentary 2018jul25
https://www.bbc.co.uk/programmes/w3csxgv9

At a time of unprecedented change and scrutiny of the media, Razia Iqbal interviews and listens again to the archive from British newspaper man Harold Evans, whose name has become a byword for serious investigative journalism. From his flat in New York, she speaks to Sir Harry about giving voice to the voiceless, risking going to prison and changing British law in his lifelong pursuit of the truth.
Last edited by tlfong01 on Fri Jul 27, 2018 6:38 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: Relay Module KY-019 5V

Thu Jul 26, 2018 9:17 am

tlfong01 wrote:
Wed Jul 25, 2018 3:08 pm
Now that I have no problem reading and writing a data byte to IOX or RTC devices, there is little problem configuring both devices to control anything, because all control is done by reading and writing registers. The programming is very tedious, but it is all about register read and write, and it is not that difficult to google Arduino program examples and borrow or steal something for RpiZW.
Tomorrow I will write simple operations such as reading RTC temperature, ...

Now I am going to read the RTC's temperature registers. I first extract the relevant parts from the datasheet, as below. Last time I test by writing a byte to Day register at address 0x03, then read it back, and print it.

This time the current temperature is always in the two temperature registers. In other words, there is no need to write, RTC chip updates it automatically (I forgot how often, should be very often). We just read the two temperature registers any time we want. I will number this version as 1.6.

I will now take a break and go to the Arduino guys' places to see if there are anything useful to steal. What I am looking for is a function to merge the two parts of the signed 10 bit temperature stored in two registers into one 10 bit binary number, then convert the 10 bit number to decimal and print it out for the human eyes to read.

Sometimes I don't steal, but first write the function myself, and then google around to see if there is any one writing better code than me (usually yes), then I will steal. I have no sense of quilt stealing Arduino things, because they are all open anyway, and they themselves also steal things from the PIC or Intel embedded guys, ...

Before visiting the Arduino sites, I thought I would first try doing the binary to decimal by hand. It is only then I can understand other people's function. I though hard but still cannot figure out which number format would DS3231 use. I first thought it should be sign magnitude, or sign and BCD. But I could not find any format that I think is best, especially how to locate the fixed binary or decimal point. At last I gave up and read the datasheet, which gives the following spec.

Then I found many of my above guesses are wrong!

DS3231 Temperature Registers (11h–12h) Datasheet page 15

Temperature is represented as a 10-bit code with a resolution of 0.25°C and is accessible at location 11h and 12h.

The temperature is encoded in two’s complement format.

The upper 8 bits, the integer portion, are at location 11h and the lower 2 bits, the fractional portion, are in the upper nibble at location 12h.

For example, 00011001 01b = +25.25°C.

Upon power reset, the registers are set to a default temperature of 0°C and the controller starts a temperature conversion.

The temperature is read on initial application of VCC or I2C access on VBAT and

once every 64 seconds afterwards.

The temperature registers are updated after each user-initiated conversion and on every 64-second conversion.

The temperature registers are read-only.
Attachments
GsLy9od[1].jpg
GsLy9od[1].jpg (58.87 KiB) Viewed 1397 times
Last edited by tlfong01 on Fri Jul 27, 2018 2:49 pm, 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: Relay Module KY-019 5V

Thu Jul 26, 2018 2:26 pm

tlfong01 wrote:
Thu Jul 26, 2018 9:17 am
Now I am going to read the RTC's temperature registers. I first extract the relevant parts from the datasheet, as below.

V1.6 working, but still buggy

It took me 2 hours to debug the read temperature function. I ran the program and the reading was 27 degrees C. I used a hot air gun to blow the DS3231 chip for 1 minute, and ran the program again. This time the reading was 31 degree C.

The readings I expected were 31 degrees and 35 degrees. I don't know why the readings were a couple of degrees below. Perhaps the conversion formula has a bug. Will check tomorrow.

The V1.6 program is list below.

The program is getting longer. Perhaps I should get a GitHub thing or some place in the Cloud to store the old versions of the program.

Code: Select all

#!/usr/bin/python3

# KY019 DS3231 Real Time Clock Test Program V1.6 tlfong01 2018jul26hkt2158

# *** Short Description ***
# 1. read chip temperature

# *** Long Description ***
# 1. read chip temperature

# *** Setup/Debug Notes ***
# 1. To change I2c speed to 100Khz
#    $ sudo nano /boot/config.txt
#    modify the dtparam=i2c_arm_on line as below 
#    dtparam=i2c_arm=on, i2c_arm_baudrate=100000
# 2. I2c speeds list
#    low speed      =  50kHz ( 50000) 
#    standard speed = 100KHz (100000)
#    maximum speed  = 400KHz (400000)
# 3. I2C speed can only be changed at raspbain boot time.  It cannot be changed using python.

# *** Import ***

import datetime
import time
import smbus

# *** Config ***

# Raspberry Pi                              = Raspberry Pi Zero W V1.1
# Raspbian                                  = Linux 9 (stretch 2018 April)
# I2c speed                                 = 100 kHz

programTitle                                = 'DS3231 RTC Example Test Program'
timeNowStr                                  = str(datetime.datetime.now())[0:16]
i2cCh1                                      = smbus.SMBus(1)

dataByte0x04                                = 0x04
dataByte0x06                                = 0x06

dvAddrByte                                  = 0x68

dayRegAddrByte                              = 0x03

# *** Device Functions ***

def readDvRegOneByte(i2cPort, dvAddrByte, dvRegAddrByte):
    readByte = i2cPort.read_byte_data(dvAddrByte, dvRegAddrByte)
    return readByte

def writeDvRegOneByte(i2cPort, dvAddrByte, dvRegAddrByte, writeByte):
    writeDviTwoBytes(i2cPort, dvAddrByte, dvRegAddrByte, writeByte)
    return

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

def printDvRegOneByteAndTitle(i2cPort, dvAddrByte, dvRegAddrByte, printTitle):
    readByte = readDvRegOneByte(i2cPort, dvAddrByte, dvRegAddrByte)
    print(printTitle, hex(readByte))

def printDataByteAndTitle(i2cPort, dvAddrByte, dataByte, printTitle):
    print(printTitle, hex(dataByte))
    return

# *** Device Operation delays ***

def clockWriteDelay():
    time.sleep(0.2)
    return

def clockReadDelay():
    time.sleep(0.2)
    return

def temperatureConversionDelay():
    time.sleep(0.25)
    return

def oscStopDelay():
    time.sleep(0.3)
    return

def pushButtonDebounceDelay():
    time.sleep(0.3)
    return

# *** Test Functions ***

def testWriteReadPrintDayReg(testDataByte):
    printDvRegOneByteAndTitle(i2cCh1, dvAddrByte, dayRegAddrByte, 'Old Byte =')
    writeDvRegOneByte(i2cCh1, dvAddrByte, dayRegAddrByte, testDataByte)    
    printDvRegOneByteAndTitle(i2cCh1, dvAddrByte, dayRegAddrByte, 'New Byte =')
    return

def test15():
    print('\n*** TimeNow = ', timeNowStr, '*********************************\n')
    print('***', programTitle, 'V1.5', 'Begin ***\n')
    print('+++ Test Write, Read, Print Day Register +++\n')
    testWriteReadPrintDayReg(dataByte0x04)
    testWriteReadPrintDayReg(dataByte0x06)
    print('\n')
    print('***', programTitle, 'V1.5', 'End  ***\n')
    return

def test16():
    print('\n*** TimeNow = ', timeNowStr, '*********************************\n')
    print('+++', programTitle, 'V1.6', 'Begin +++\n')
    print('+++ Test Read Temperature Registers +++\n')

    # *** Get most significant and least significant byte of temperature ***
    msByteRegAddr = 0x11
    lsByteRegAddr = 0x12
    msByte = readDvRegOneByte(i2cCh1, dvAddrByte, msByteRegAddr)
    temperatureConversionDelay()    
    lsByte = readDvRegOneByte(i2cCh1, dvAddrByte, lsByteRegAddr)
    temperatureConversionDelay()
    printDataByteAndTitle(i2cCh1, dvAddrByte, msByte, 'Most  Significante Byte =')
    printDataByteAndTitle(i2cCh1, dvAddrByte, lsByte, 'Least Significante Byte =')

    # *** Convert to decimal ***
    tempInteger  = msByte   
    tempFraction = (lsByte >> 6) * 25
    tempStr      = str(tempInteger) + '.' + str(tempFraction)
    print('\ntemperature =', tempStr, 'degrees C')
    print('\n')
    print('***', programTitle, 'V1.6', 'End  ***\n')
    return

# *** Main ***

test15()
test16()

# *** Sample Output ***

'''
*** TimeNow =  2018-07-26 22:03 *********************************

*** DS3231 RTC Example Test Program V1.5 Begin ***

+++ Test Write, Read, Print Day Register +++

Old Byte = 0x6
New Byte = 0x4
Old Byte = 0x4
New Byte = 0x6


*** DS3231 RTC Example Test Program V1.5 End  ***


*** TimeNow =  2018-07-26 22:03 *********************************

+++ DS3231 RTC Example Test Program V1.6 Begin +++

+++ Test Read Temperature Registers +++

Most  Significante Byte = 0x1b
Least Significante Byte = 0xc0

[color=#FF0000][b]temperature = 27.75 degrees C[/b][/color]


*** DS3231 RTC Example Test Program V1.6 End  ***

*** DS3231 RTC Example Test Program V1.5 End  *** [b][color=#FF0000]<<< use hot air gun to increase chip temperature !!![/color][/b]


*** TimeNow =  2018-07-26 22:07 *********************************

+++ DS3231 RTC Example Test Program V1.6 Begin +++

+++ Test Read Temperature Registers +++

Most  Significante Byte = 0x1f
Least Significante Byte = 0x80

[color=#FF0000][b]temperature = 31.50 degrees C[/b][/color]


*** DS3231 RTC Example Test Program V1.6 End  ***
'''

# *** 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: Relay Module KY-019 5V

Thu Jul 26, 2018 2:44 pm

davidcoton wrote:
Mon Jul 23, 2018 4:59 pm
1. And try not to post the same code many times, it takes up space (both server space and page space) for no good purpose. Just post the day's new stuff, unless previous code has had a significant revision. Then re-post the whole thing at the end. (This is my preference, others, including the Mods, may have different ideas.)

2. a long stalled heating controller

Two short questions

1. My programs are getting longer and longer. Listing programs here gets messy. I am thinking of putting the old posts in the GitHub. I wonder if there are already Rpi Forum users using Git Hub or GitLab?
I opened a GitHub account some years ago, but it is almost empty because I don't have useful things to put there for the public. Or perhaps I should try GigLab which I think is more suitable for beginners.

2. What is the meaning of "long stalled controller"? My first guess is a controller usually idle, and only operates for a short time when needs to. But that does not make much sense to me.
I am an electronics and smart home hobbyist.

User avatar
davidcoton
Posts: 4258
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: Relay Module KY-019 5V

Thu Jul 26, 2018 7:53 pm

tlfong01 wrote:
Thu Jul 26, 2018 2:44 pm
davidcoton wrote:
Mon Jul 23, 2018 4:59 pm
1. And try not to post the same code many times, it takes up space (both server space and page space) for no good purpose. Just post the day's new stuff, unless previous code has had a significant revision. Then re-post the whole thing at the end. (This is my preference, others, including the Mods, may have different ideas.)

2. a long stalled heating controller

Two short questions

1. My programs are getting longer and longer. Listing programs here gets messy. I am thinking of putting the old posts in the GitHub. I wonder if there are already Rpi Forum users using Git Hub or GitLab?
I opened a GitHub account some years ago, but it is almost empty because I don't have useful things to put there for the public. Or perhaps I should try GigLab which I think is more suitable for beginners.

2. What is the meaning of "long stalled controller"? My first guess is a controller usually idle, and only operates for a short time when needs to. But that does not make much sense to me.
Personally, I have no experience of GitHub or GitLab, but I think one or the other will work for you.

By "long stalled", I mean I started work, got busy on other things, and need to get back to it after a two year :!: break.
Signature retired

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

Re: Relay Module KY-019 5V

Fri Jul 27, 2018 5:57 am

davidcoton wrote:
Thu Jul 26, 2018 7:53 pm
tlfong01 wrote:
Thu Jul 26, 2018 2:44 pm
davidcoton wrote:
Mon Jul 23, 2018 4:59 pm
1. And try not to post the same code many times ...
2. a long stalled heating controller ...
Two short questions
1. I wonder if there are already Rpi Forum users using Git Hub or GitLab?
2. What is the meaning of "long stalled controller"?
GitHub or GitLab, ... but I think one or the other will work for you.
By "long stalled", I mean I started work, got busy on other things, and need to get back to it after a two year :!: break.

Many thanks for your advice and English lesson. Perhaps I can try uploading my outdated programs to GitHub. I would also google Rpi Forum to check if there are already users setting up GitHub in home automation.

I have a list of long stalled projects. Usually a project stalled longer than 5 years will be automatically deleted, because I cannot remember things that long. But one 10 year old project I still remember is an Arduino Interrupt project which never completed. I started with while loops and then switched to interrupts, but ended up with a big mess. That is why earlier I said I wanted a Minimal, Complete, Verifiable, NoPorgram Example on Rpi interrupt.

Anyway, I will also google any Rpi interrupt examples later.
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: Relay Module KY-019 5V

Fri Jul 27, 2018 6:36 am

tlfong01 wrote:
Thu Jul 26, 2018 6:58 am
My 70cm x 40cm work bench is getting overcrowded ...
two new comers, a heater and a fan ...

And the last member of the family, a AC220V, 1,000W induction cooker

Homey Germany DTM-200 1000W Mini Cooker - HK$269
http://electromart.hk/c.php?page=3&filt ... ber=CO4068

Power 1000W
Microcomputer control, security lock
Touch button switch, quick key design, simple and easy operation
Black quartz heat resistant panel
EMI anti interference protection mechanism, not interferenced by other electrical appliances at the same time.
Function: Hot port, making tea, sauteing, frying, keeping warm
Attachments
0jmJZi5[1].jpg
0jmJZi5[1].jpg (57.08 KiB) Viewed 1322 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: Relay Module KY-019 5V

Fri Jul 27, 2018 7:29 am

tlfong01 wrote:
Fri Jul 27, 2018 5:57 am
I would also google Rpi Forum to check if there are already users setting up GitHub in home automation.
Anyway, I will also google any Rpi interrupt examples later.

I searched Rpi Forum and found the following thread useful.

AUTOMATION AND CONTROL - Green House Temperature Control - sampazzer 2018-Jul-26
viewtopic.php?t=219042

temperature control a greenhouse, other sensors

to view the temperature and change the required set point.

upload this data to a database so I can trend the values.

a single Pi got so caught up and stuck using multithreading/multiprocessing I threw the towel in.

while true control loop monitors temp, compares against setpoint, opens window or starts heater dependant on the result.

This takes up all processor power and means there's no chance of running any GUI / IoT code without using multiple threads. I also then struggled to share variables between threads using queues ect.

Having re-thought this. Would it be easier to have two Pi's communicating over serial.

One to record the sensors and perform the control loop and the other to deal with sending setpoint data and recieving sensor data to display on the GUI and database control?


RE: AUTOMATION AND CONTROL Post by Idahowalker » 2018-Jul-27
viewtopic.php?t=219042#p1345713

Instead of looping and sleeping, how about using interrupts?


RE: AUTOMATION AND CONTROL Postby Idahowalker » 2018-Jul-27 Fri 11:08 am
viewtopic.php?t=219042#p1345721

An RTC and using alarms, that you configure through code, is one way.

Setting the RTC to cause an interrupt once every Xminutes where the program could then take action; based upon the alarm receipt.

An, inexpensive, DS1307 would do the trick.

I have my RTC set to send my program a 1PPS interrupt. Upon receipt of the 1PPS signal, counters are incremented for several threads. The threads are held in a wait by a threading.event. When a particular counter reaches a predetermined count the threading.event is released, the thread does its ThingDo, and the ThingDo is reset so the whole show can start all over.


---

Youtube and Magazine

Solar Power Rpi Monitor Control Greenhouse Feb 23, 2018
https://www.youtube.com/watch?v=x4YHtTZoFhU

CPU Cooling Rpi Built-In CPU Sensor Fans In Greenhouse Mar 18, 2018
https://www.youtube.com/watch?v=B2f86BTa9Cg

UPS As An Inverter Rpi Controlled 110 AC Fan Mar 14, 2018
https://www.youtube.com/watch?v=cfdNkWKtlXY

Rpi Automatic Control Hydronic Exchange Mar 9, 2018
https://www.youtube.com/watch?v=3-BqUfC8VEs

Home Automation with a Raspberry Pi and MQTT, Part 1 Aug 10, 2017
https://www.youtube.com/watch?v=yXvgVgxXjbM

Automatic Vent Rpi Controll Greenhouse Part 2 Mar 18, 2018
https://www.youtube.com/watch?v=k3sXz9oQYOc&t=113s

MagPi Issue 18 Nov2013 (Plant-monitoring system)
https://www.raspberrypi.org/magpi-issues/MagPi18.pdf

The moisture sensor - 74HC14

/ to continue, ...
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: Relay Module KY-019 5V

Fri Jul 27, 2018 9:32 am

tlfong01 wrote:
Thu Jul 26, 2018 2:26 pm
V1.6 working, but still buggy
It took me 2 hours to debug the read temperature function. I ran the program and the reading was 27 degrees C. I used a hot air gun to blow the DS3231 chip for 1 minute, and ran the program again. This time the reading was 31 degree C.

The readings I expected were 31 degrees and 35 degrees. I don't know why the readings were a couple of degrees below. Perhaps the conversion formula has a bug. Will check tomorrow.

This afternoon I started troubleshooting the RTC temperature reading a couple of degrees below expectation problem. I have the following options.

  • 1. Walk through the binary to decimal conversion function to detect any possible bug.
  • 2. Try not to use RTC generated temperature values, but break the function in the middle and input my own fake temperature values.
  • 3. Swap RTC module and run the module again.
I thought the conversion function is straightforward, so it is not likely to have a bug. Using hot air gun to increase temperature gave an increase in reading, but consistently a couple of degrees below expected.

So I decided to first do swap module testing. I swapped the module and ran the same V1.6 program. I surprisingly found the reading is 32 degrees! So problem solved. Winner Winner Chicken Dinner. I originally expected 3 hours doing a nasty troubleshooting. I did not expect to find the cause (bad reading RTC). The datasheet did not seem to have an option to tune the temperature sensor. Perhaps I should read it again.

Lesson learnt - try swapping first!

To do - stock perhaps 4 or 5 more RTC modules (9 yuan each) for more swapping test later.


Selfie brain storming - I already chose a reliable TaoBao shop for the RTC modules. Not sure if even good shops sell bad (rejected temperature) modules. Or they assume almost all users don't read the temperature, but use a temperature chip. That is a reasonable assumption, because RTC gives the chip temperature, nor room temperature. Perhaps I should not be lazy, and use a special purpose temperature sensing module.
Attachments
BvAve3P[1].jpg
BvAve3P[1].jpg (103.97 KiB) Viewed 1290 times
Last edited by tlfong01 on Sat Jul 28, 2018 12:32 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: Relay Module KY-019 5V

Fri Jul 27, 2018 12:31 pm

tlfong01 wrote:
Fri Jul 27, 2018 9:32 am
Selfie brain storming - I already chose a reliable TaoBao shop for the RTC modules. Not sure if even good shops sell bad (rejected temperature) modules. Or they assume almost all users don't read the temperature, but use a temperature chip.
I don't want to wrongly accuse the 90% chance defective RTC chip or TaoBao shop. So I think I should swap back and see if the bad chip become good again. Swapping two 5-pin Dupoint connectors takes less than 15 seconds, anyway.

So I did a double swap - module 1 to module 2 circuit, and vice versa. I found Module 1 still reads 28 degrees C, Module 2 still 30. This confirm it is not because of other factors such as power, long wire, poor contact somewhere. 28 is still 28, 30 is still 30. I decided to test another module #3 to compare, if #1 is 28, #2 and #3 are 30, then very likely #1 is bad. To my surprise, #3 is 32 degrees C. The readings differ by 4 degrees at most, but they are stable. Then I test yet one more #4. #4 gives 30. So the 4 modules give 28, 30, 32, 30, all stable readings. I immediately take the reading after inserting the module, so there should be very little heating up.

So I have WRONGLY accused the first module and the TaoBao shop. All modules and the shop are good. It is only that the modules are not tuned correctly, and I have not read the datasheet closely enough to find if any accuracy of temperature is guaranteed.

So everything still go smoothly, though now I know the RTC temperature reading are stable, but not accurate. I might use a accurate temperature sensor later. But for prototyping stage, now, I can move on and try more new things.
Attachments
MWvSYgh[1].jpg
MWvSYgh[1].jpg (70.23 KiB) Viewed 1265 times
Last edited by tlfong01 on Fri Jul 27, 2018 2:43 pm, edited 2 times in total.
I am an electronics and smart home hobbyist.

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

Re: Relay Module KY-019 5V

Fri Jul 27, 2018 1:23 pm

What are the specifications of the modules?

I would imagine there would be a tolerance range to the accuracy. If it was +/-10% say, then all of your modules would be within spec for a temperature reading of 30C, i.e. 2 are bang on, one reads high and one reads low.
--------------
The purpose of a little toe is to ensure you keep your furniture in the right place.

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

Re: Relay Module KY-019 5V

Fri Jul 27, 2018 2:33 pm

tlfong01 wrote:
Thu Jul 26, 2018 6:58 am
So I have stacked up the 3 MCP23017 together, to squeeze more space for two new comers, a heater and a fan.
Whirlpool Ceramic Heater, HT015 (IP21), 1500W
https://www.whirlpool.com.hk/product/de ... er/ht015_1
IMARFLEX 10H-E048 HK$148
http://www.my29544668.com/index.php?p=6&id=1526

Now I have inserted the heater and the fan into the schematic.
Attachments
NZrHEbw[1].jpg
NZrHEbw[1].jpg (169.56 KiB) Viewed 1238 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: Relay Module KY-019 5V

Fri Jul 27, 2018 3:26 pm

Mortimer wrote:
Fri Jul 27, 2018 1:23 pm
What are the specifications of the modules?

I would imagine there would be a tolerance range to the accuracy. If it was +/-10% say, then all of your modules would be within spec for a temperature reading of 30C, i.e. 2 are bang on, one reads high and one reads low.

But the spec says this extremely accurate, that extremely accurate, so everything should be extremely accurate, everything 3ppm, 5ppm etc. Nothing should be inaccurate. Anyway, let me go check what the friendly datasheet says, ...

The datasheet says I am not a pro, there is a long long way to go, ... :(

Anyway, I think it's time to call it a day.
Attachments
hKUJmkd[1].jpg
hKUJmkd[1].jpg (99.81 KiB) Viewed 1224 times
Last edited by tlfong01 on Sat Jul 28, 2018 12:24 am, edited 1 time in total.
I am an electronics and smart home hobbyist.

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

Re: Relay Module KY-019 5V

Fri Jul 27, 2018 3:54 pm

Ah but all of that relates to the accuracy of the real time clock circuit.

So "+/-2ppm from 0 to 40degC" tells you that between those temperatures the accuracy of the RTC is better than 2ppm. It says nothing about the accuracy of the temperature sensor.

The +/-2degC temperature accuracy is more like it. Your temperature readings covered a range of 4degC, which tallies with the claimed accuracy doesn't it?

Something else to bear in mind is the purpose of that temperature sensor. It is there to measure the internal temperature of the RTC chip, so it can compensate for thermal effects on the voltage reference and comparator circuit, not to measure ambient air temperature. To be fair it won't be far out, but you might need to compensate for the internal chip temp being higher than ambient.
--------------
The purpose of a little toe is to ensure you keep your furniture in the right place.

Return to “Automation, sensing and robotics”