Pico W Micro Python for SHT30 Temp/Humidity HELP!

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
Rissy
Posts: 116
Joined: Sun Aug 14, 2022 8:15 am

Pico W Micro Python for SHT30 Temp/Humidity HELP!

Post by Rissy » Sun Aug 14, 2022 8:33 am

Hello all!

This is my very first forum post after joining literally minutes ago.

I've recently decided to dive into the world of Pico W after donig the same thing with my first ever RP (4B) only 3 weeks ago.

I'm working on a project where I now have some weather type measurements being recorded by my 4B but now I wish to add to that with my newly received Pico W. I'm wishing to interface this new Pico W with the below linked item (SHT30 Temp/Humidity Sensor) to compliment my already working indoor sensors.
https://thepihut.com/products/sht-30-me ... ity-sensor

After looking at my Pico W for the majority of the day yesterday, I have to admit that this a completely different kettle of fish from that of programming my RP4B usinig Python (I'm a noob at that, but now have a few weeks experience under my belt).

So far, by way of following instruction rather than fully understanding the code I've been writing, I've managed to connect my Pico W to my home wifi network and it be assigned an IP address from my router (DHCP i guess). I've managed to flash the onboard LED a few times, and I've manaaged to read the internal temperature of the Pico W itself. So then I thought, "right, time to get it talking on serial (I2C) to the SHT30 I've bought."

This is where I've fallen completely on my face with a thud!

It seems that NOBODY ANYWHERE in all these years has ever written a driver file and interface file for a Pico (anything) in micropython for one of these devices!

I'm not clever enough yet to come up with something any time soon to achieve this, so though perhaps I might be able to tap into you clever bods as a resource for me progessing my project along and not losing interest.

Can anyone help me?

There's lots more clever stuff after this I'll be needing help with to fulfil my project desires later, but lets leave those things until I can actually talk to the sensor first!

I'm a complete Pico nooby (is that the word?) (groan and drone i hear you make noises), so please, be gentle.

Thanks in advance

Rissy
Posts: 116
Joined: Sun Aug 14, 2022 8:15 am

Re: Pico W Micro Python for SHT30 Temp/Humidity HELP!

Post by Rissy » Sun Aug 14, 2022 8:51 am

I should also add that I am trying to program the Pico uisng a Windows 10 laptop and Thonny whilst I leave my RP4B doing its thing.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Pico W Micro Python for SHT30 Temp/Humidity HELP!

Post by Roberthh » Sun Aug 14, 2022 8:56 am

How about this one: https://github.com/rsc1975/micropython-sht30
or here in a slighty newer version: https://libraries.io/pypi/micropython-sht30

A short search reveals many versions and variants of a driver.

Rissy
Posts: 116
Joined: Sun Aug 14, 2022 8:15 am

Re: Pico W Micro Python for SHT30 Temp/Humidity HELP!

Post by Rissy » Sun Aug 14, 2022 12:50 pm

Thanks for that Roberthh,

Much appreciated receiving some feedback, and so quickly too! I'll have a play with the first link code you provided.

I'll admit i don't think I saw those even after HOURS yesterday trying to find a solution, but maybe they don't appear just using a google search...? Not sure how you found these...?

In the meantime while I do that... --->

So let me ask this question. Bearing in mind i'm on a Windows 10 laptop, and looking at your links; especially in the case of your second link; when they request you to "pip install micropython-sht30==0.2.7" HOW does one do that on a Windows laptop?

In fact, I'm loaded with questions actually. Like I said; i'm brand new at this. When i open Thonny, I can write code, and save this as a .py on to Windows, or on to the Pico itself. They seems to survive a power down when put on to the Pico in this manner. However, how does one run these programs automatically rather than using the green "go" button on Thonny? If you have multiple programs stored on to the Pico, how do you know how to only run one of them perhaps?

When i drag and drop "rp2-pico-w-20220812-unstable-v1.19.1-283-g6f4d424f4.uf2" on to the Pico by powering it up like a USB memory stick using the onboard button, this allows me to program the Pico and play with code etc, but this does NOT survive a power cycle!? I have to keep dragging it back on to the Pico evry time I power it down by unplugging it from my USB. Is this correct? If so, then this makes me beg the question "how can i run my programs automatically if when i power it down, the programming environment is gone with it!?"

As you can see, i'm full of confusion and questions....!

Rissy
Posts: 116
Joined: Sun Aug 14, 2022 8:15 am

Re: Pico W Micro Python for SHT30 Temp/Humidity HELP!

Post by Rissy » Sun Aug 14, 2022 1:15 pm

Here's another question.

According to this variant, there's "pull up" resistors on the board: https://learn.adafruit.com/adafruit-sht ... ut/pinouts

However, Mine doesn't look like this. Min looks like this: https://thepihut.com/products/sht-30-me ... ity-sensor

Does this mean I still need to buy some 10k resistors for this sensor to work with my Pico W?

Also, about addresses. This sensor it says can be 0x44 or 0x45.

QUOTE from first link "ADR - This is the I2C address selection pin. This pin has a 10K pull down resistor to make the default I2C address 0x44. You can tie this pin to Vin to make the address 0x45".

How would this work with my device seeing as I have no visible pins. Is it address 0x44 or 0x45? Can i even buy another one and make it the other address from default?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Pico W Micro Python for SHT30 Temp/Humidity HELP!

Post by Roberthh » Sun Aug 14, 2022 1:23 pm

a) Yes, you will need the external pull-up resistors. When this sensor is supplied by Adafruit, the ship the resistors with the sensor. They cost just a few cents, so getting them is the trouble.
b) You can tell which addresses are on the bus by calling i2c.scan(). That call will return a list of addresses of the devices on the bus, as decimal values. So 0x44 is 68 decimal, 0x45 is 69. If the pull-up resistors are missing, the list is eiter empty or included every probed address.

Rissy
Posts: 116
Joined: Sun Aug 14, 2022 8:15 am

Re: Pico W Micro Python for SHT30 Temp/Humidity HELP!

Post by Rissy » Sun Aug 14, 2022 1:38 pm

Okay, so how does one do an I2c scan thingy? That command does not work on either SSH or within Thonny.

>>> i2c.scan()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'i2c' isn't defined

=====================

It's annoying because the PiHut said nothing about me having to buy additional resistors. Is it 1 or more 10kohm resistors I need? How do i connect them to the sensor? I only have four wires: VDD 3.3V (red) GND (black) SCL (Yellow) and SDA (Green)

I've connected my wires like this to my Pico:

VDD (3v3 on Pin 36)
GND Pin 38
SCL Pin 5
SDA Pin 4

Is that correct?

=====================

The sht30.py code from that link you provided seems to have an error or something. When i put together some code to try to access it based on their own suggestions:

from sht30 import SHT30
sensor = SHT30() <-------------------------------------------------------------------------The error doesn't like this line for some reason
temperature, humidity = sensor.measure()
print('Temperature:', temperature, 'ºC, RH:', humidity, '%')
print('Is connected:', sensor.is_present())
print('Status register:', bin(sensor.status()))
print('Single bit check, HEATER_MASK:', bool(sensor.status() & SHT30.HEATER_MASK))

From sht30.py
def __init__(self, scl_pin=5, sda_pin=4, delta_temp = 0, delta_hum = 0, i2c_address=DEFAULT_I2C_ADDRESS): <--Error with this line
self.i2c = I2C(scl=Pin(scl_pin), sda=Pin(sda_pin))
self.i2c_addr = i2c_address
self.set_delta(delta_temp, delta_hum)
time.sleep_ms(50)

I get the following error:

>>> %Run -c $EDITOR_CONTENT
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
File "sht30.py", line 43, in __init__
TypeError: 'id' argument required
>>>

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Pico W Micro Python for SHT30 Temp/Humidity HELP!

Post by Roberthh » Sun Aug 14, 2022 1:51 pm

To get a scan, type:

Code: Select all

from machine import I2C, Pin

i2c=I2C(0, sda=Pin(4), scl=Pin(5))
print(i2c.scan())
The two pull-up resistsors have to be connected between a signal wire and Vcc: sda and Vcc (1) and scl and Vcc(2).

About the error: In line 40, the ID is missing. It must read:
self.i2c = I2C(0, scl=Pin(scl_pin), sda=Pin(sda_pin))

A note: The Pin(x) numbers are GPIO numbers, not board numbers!

Rissy
Posts: 116
Joined: Sun Aug 14, 2022 8:15 am

Re: Pico W Micro Python for SHT30 Temp/Humidity HELP!

Post by Rissy » Sun Aug 14, 2022 2:06 pm

Roberthh wrote:
Sun Aug 14, 2022 1:51 pm
To get a scan, type:

Code: Select all

from machine import I2C, Pin

i2c=I2C(0, sda=Pin(4), scl=Pin(5))
print(i2c.scan())
Ah right. okay, I did this, and I got "[]" in response, so i guess this means it didn't detect anything.
Roberthh wrote:
Sun Aug 14, 2022 1:51 pm
The two pull-up resistsors have to be connected between a signal wire and Vcc: sda and Vcc (1) and scl and Vcc(2).
So, I need:
Vcc and SDA connected with a 10kohm resistor for 0x44
Vcc and SCL connected with a 10kohm resistor for 0x45

Is this correct?
Roberthh wrote:
Sun Aug 14, 2022 1:51 pm
About the error: In line 40, the ID is missing. It must read:
self.i2c = I2C(0, scl=Pin(scl_pin), sda=Pin(sda_pin))
Excellent, thanks for this. I've found this a lot with trying to program my RP4B. People's code on Github isn't always correct, which makes you think have they not had it running themselves?! Anyway, this change removed that first error. Now i get new errors which i suspect are now to do with not having a visibly connected / incorrectly connected sensor.
Roberthh wrote:
Sun Aug 14, 2022 1:51 pm
A note: The Pin(x) numbers are GPIO numbers, not board numbers!
So does this mean:
I need to pull my yellow SCL off pin 5 and put it on to GP5 (Pin 7)?
I need to pull my green SDA off pin 4 and put it on to GP4 (Pin 6)?

Rissy
Posts: 116
Joined: Sun Aug 14, 2022 8:15 am

Re: Pico W Micro Python for SHT30 Temp/Humidity HELP!

Post by Rissy » Sun Aug 14, 2022 2:15 pm

Ooooh.

I just changed my Yellow SCL on to GP5 and Green SDA wires onto GP4.

Then I ran your program again:

from machine import I2C, Pin
i2c=I2C(0, sda=Pin(4), scl=Pin(5))
print(i2c.scan())

THIS TIME, I got a better response. I got "[68]" which is Decimal 0x44!!! :D

Much better!

However, I am still getting the same new errors as before:

Traceback (most recent call last):
File "<stdin>", line 5, in <module>
File "sht30.py", line 140, in measure
File "sht30.py", line 105, in send_cmd
SHT30Error: Bus error

So that's the following:

temperature, humidity = sensor.measure()

and

raise SHT30Error(SHT30Error.BUS_ERROR)

and

data = self.send_cmd(SHT30.MEASURE_CMD, 6);

Any ideas on these three lines?

Post Reply