Power I2C sensor from GPIO pin: not enough power to refresh?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
djipey
Posts: 21
Joined: Sun Dec 01, 2019 3:04 pm

Power I2C sensor from GPIO pin: not enough power to refresh?

Post by djipey » Sat Jan 30, 2021 5:21 pm

Hello,

I have an ESP12E, 4MB flash loaded with micropython. I would like to read from an I2C sensor. I can do this without trouble when the power pin of the I2C sensor (a BMP280) is connected directly to the power pin of the board.

However, I would like to connect the power pin of the sensor to one of the board's GPIO. This is to power the sensor only when the board is awake. Here is the code I have so far:

Code: Select all

import json
import machine
import time
import ubinascii

# Personal module
import config

power_pin = machine.Pin(4, machine.Pin.OUT)
power_pin.on()
# time.sleep(1)

i2c = machine.I2C(scl=machine.Pin(config.SCL_PIN), sda=machine.Pin(config.SDA_PIN))
temp_sensor = config.setup_sensors(i2c)

power_pin.off()

while True:

    power_pin.on()
    time.sleep(5)

    try:
        temp_sensor = config.setup_sensors(i2c)
        temperature, pressure, humidity = temp_sensor.values
        temperature = float(temperature.replace("C", ""))
        pressure = float(pressure.replace("hPa", ""))
        humidity = float(humidity.replace("%", ""))
        print("Temperature, pressure, humidity:", temperature, pressure, humidity)
    except Exception as e:
        print("BME280 reading failed with: {}".format(e))

    power_pin.off()

    payload = {"temperature": temperature, "humidity": humidity, "pressure": pressure}

    print(payload)

    print("going to sleep")
    # machine.deepsleep()
    time.sleep(5)

Code: Select all

power on or hard reset
Sensors ready
Sensors ready
Temperature, pressure, humidity: 20.02 657.64 63.16
{'humidity': 63.16, 'pressure': 657.64, 'temperature': 20.02}
going to sleep
Sensors ready
Temperature, pressure, humidity: 20.02 657.64 63.16
{'humidity': 63.16, 'pressure': 657.64, 'temperature': 20.02}
going to sleep
Sensors ready
Temperature, pressure, humidity: 20.02 657.64 63.16
{'humidity': 63.16, 'pressure': 657.64, 'temperature': 20.02}
going to sleep
Sensors ready
Temperature, pressure, humidity: 20.02 657.64 63.16
{'humidity': 63.16, 'pressure': 657.64, 'temperature': 20.02}
going to sleep
I'm using the GPIO 4 from the board to power the sensor. Somehow, my code doesn't crash, but I always get the measurement from the sensor. I assume it's because the registers on the sensors board don't get updated?

Could you please provide an explanation as to why this is happening, and how I can solve this problem?

Kind regards

Post Reply