Apologies if this is not a pico question, not sure where the problem lies...
I have a small hobby motor (5v) mounted with a hobby fan:
that is connected to Normally Open on a relay and 5V Wall Power supply (via a barrel/terminal block adapter. I also hooked up an oled and a temp probe:
 and 5V Wall Power supply (via a barrel/terminal block adapter. I also hooked up an oled and a temp probe:
On the input side, the relay connections are:
- VCC to Pi Pico 3.3V/pin 36
- GND to GND
- input pin to GPIO 15
Everything works well as I am pursing a small thermostat project following these links:
https://www.youtube.com/watch?v=mj2kMD0LCR4&ab_channel=CoreElectronics
https://xilent.co.uk/2021/02/19/pi-pico-thermostat/
However, after triggering the relay to turn on the fan, OR after about 10 loops of just turning the relay on / off with a delay of 5 seconds, Thonny no longer prints to the screen and can no longer connect to /dev/ttyACM0.
Here's the simple test code:
import time
from machine import Pin, I2C
from oled_runner import lcd
i2c0_sda = Pin(12)
i2c0_scl = Pin(13)
i2c0 = I2C(0, sda=i2c0_sda, scl=i2c0_scl)
from dht20 import DHT20
dht20 = DHT20(0x38, i2c0)
relay = Pin(15, Pin.OUT)
relay.value(1)
relay_sleep=5
count=0
measurements=0
def run_relay():
    global count
    print('sleep before turn on relay')
    time.sleep(relay_sleep)
    relay.value(0)
    print('sleep before turn off  led')
    time.sleep(relay_sleep)
    relay.value(1)
    count+=1
    print(count)
def thermo_stat():
    if fahrenheit > 77:
        #If on, turn off (relay.value(0) sets GPIO high)
        if relay.value() == 1:
            print("Turning on the fan via the relay")
            relay.value(0)
        else:
            print("Relay value already", relay.value())
    else:
        print("Turning off the fan via the relay")
        relay.value(1)
def oled_show():
    lcd.fill(0)
    lcd.show()
    lcd.text(f"Temp: {fahrenheit} F",0,0)
    lcd.show()
def thermometer():
    global fahrenheit
    measurements = dht20.measurements
    celcius = measurements['t']
    fahrenheit = (celcius * 1.8) + 32
    fahrenheit = round(fahrenheit, 2)
    print(f"Temp: {fahrenheit} °F, humidity: {measurements['rh']} %RH")
while True:
    thermometer()
    oled_show()
    print("Wait...")
    time.sleep(5)
    gc.collect()
    thermo_stat()
    #run_relay() -   toggle this on/of per testing ...
    sleep(5)
The pico continues to 'be a thermostat' if the temp goes up/down and can still turn the relay on/off, it's just Thonny can't talk to the Pico at all anymore. The Linux PC powering the USB connection can see the pico via 'lsusb'. I don't see any obvious errors in the backend.log on Thonny.
I noticed this only happens when the relay is turned on, otherwise if the temp never trips the relay Thonny/Pico still communicate.
Suggestions? It seems like a Thonny problem but it more seems like the pico becomes unresponsive on the usb once the relay is triggered.



 
    