SOLVED: Raspberry Pico 5V pin 40 relay motor plant watering

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
LKiwi
 
Posts: 6
Joined: Thu May 27, 2021 5:26 am

SOLVED: Raspberry Pico 5V pin 40 relay motor plant watering

Post by LKiwi »

First post and fairly new to this.
Trying to create a simple watering system, not quite finished yet. Got it going.
Currently connected to Mac via USB, though seems to lock when the program is run after a few seconds when the pump is powered, if I switch off the external pump power sublimed by three AA rechargeable batteries it is fine.
With the battery pack connected to the pump, Mu stops responding, or rather the Pico does?? and I have to unplug the usb to stop the program running and reset.
Any suggestions or errors I am making? The code is not tidy or complete yet.

Parts List
Pico: https://thepihut.com/products/raspberry-pi-pico
Relay board: https://thepihut.com/products/gravity-relay-module-v3-1
Capacitive soil moisture: https://thepihut.com/products/capacitiv ... ure-sensor
Water Pump:https://thepihut.com/products/submersib ... tical-type

Photos: https://www.icloud.com/photos/#0JNuWUAM ... etruibDI_g

# code here :-)

Code: Select all

from machine import Pin
import machine
import utime
SENSOR_MIN = 38000
SENSOR_MAX = 58500

analog_value = machine.ADC(26)

relay1 = Pin(2, Pin.OUT)

while True:
    reading = analog_value.read_u16()
#   print("ADC: ",reading)
    percent = round(((reading - SENSOR_MAX) / (SENSOR_MIN - SENSOR_MAX)) * 100)
    print("Percent:", percent)

    if (percent == 0):
        print("Alert, No Water!")
        utime.sleep(1)

    if (percent >= 70):
        print("Alert, Too Much Water!")
        utime.sleep(1)

    if (percent <= 30):
        print("Watering")
        relay1.toggle()
        utime.sleep(.5)
        relay1.toggle()
        utime.sleep(2)


    utime.sleep(2)
Last edited by Franklin97355 on Fri Jun 11, 2021 10:54 am, edited 2 times in total.
Reason: added [code] tags

User avatar
blnkjns
 
Posts: 963
Joined: Fri Oct 02, 2020 3:33 am

Re: Raspberry Pico 5V pin 40 relay motor plant watering

Post by blnkjns »

Please post with code tags so the indentation does not get lost.

User avatar
LKiwi
 
Posts: 6
Joined: Thu May 27, 2021 5:26 am

Re: Raspberry Pico 5V pin 40 relay motor plant watering

Post by LKiwi »

blnkjns wrote:Please post with code tags so the indentation does not get lost.
BANNED link below
https://BANNED.com/KmRNa0Uh

User avatar
LKiwi
 
Posts: 6
Joined: Thu May 27, 2021 5:26 am

Re: Raspberry Pico 5V pin 40 relay motor plant watering

Post by LKiwi »

This may maybe more hardware related, as when I turn on the battery power for the pump, Mu locks, if I turn battery power off, Mu functions again. So I am wondering if it has to do with pin 40 which powers the relay??

Also I cannot get it to run away from the Mac, e.g. usb connected to wall or usb connected to power bank, both at 5V

User avatar
blnkjns
 
Posts: 963
Joined: Fri Oct 02, 2020 3:33 am

Re: Raspberry Pico 5V pin 40 relay motor plant watering

Post by blnkjns »

Maybe the relay pulls too much from the Pico and shuts the chip off.
You could use a transistor to power it, or use the transistor to run the motor directly.
Regarding your code: always include a marging for the motor. In your program, the motor will add water until the value is 30, and then it can immediately drop, kicking the motor in again, resulting in some stuttering. You better built in a variable that becomes true when watering, but that will become only false again when the level drops below say 25. You will need to test some values.

User avatar
LKiwi
 
Posts: 6
Joined: Thu May 27, 2021 5:26 am

Re: Raspberry Pico 5V pin 40 relay motor plant watering

Post by LKiwi »

blnkjns wrote:Maybe the relay pulls too much from the Pico and shuts the chip off.
You could use a transistor to power it, or use the transistor to run the motor directly.
Regarding your code: always include a marging for the motor. In your program, the motor will add water until the value is 30, and then it can immediately drop, kicking the motor in again, resulting in some stuttering. You better built in a variable that becomes true when watering, but that will become only false again when the level drops below say 25. You will need to test some values.
Thank you, yes you are correct, I will try adding a margin for pump triggering. Possibly the relay is pulling too much though it should not be as correctly rated for this purpose. I read somewhere that someone suggested putting a 1K resistor on pin 40 for this?? Ideally I do not want to use the transistor as wanted to use a relay, so I could use external power. I could use a mofset, though really want to get this going.
Thanks again

User avatar
LKiwi
 
Posts: 6
Joined: Thu May 27, 2021 5:26 am

Re: Raspberry Pico 5V pin 40 relay motor plant watering

Post by LKiwi »

LKiwi wrote:
blnkjns wrote:Maybe the relay pulls too much from the Pico and shuts the chip off.
You could use a transistor to power it, or use the transistor to run the motor directly.
Regarding your code: always include a marging for the motor. In your program, the motor will add water until the value is 30, and then it can immediately drop, kicking the motor in again, resulting in some stuttering. You better built in a variable that becomes true when watering, but that will become only false again when the level drops below say 25. You will need to test some values.
Thank you, yes you are correct, I will try adding a margin for pump triggering. Possibly the relay is pulling too much though it should not be as correctly rated for this purpose. I read somewhere that someone suggested putting a 1K resistor on pin 40 for this?? Ideally I do not want to use the transistor as wanted to use a relay, so I could use external power. I could use a mofset, though really want to get this going.
Thanks again
On inspection the relay board has a transistor setup to activate it with resistors... it is Opto isolated. what I can't figure is when I plug in a 5v Pi2 power supply the pico doesn't start??

User avatar
LKiwi
 
Posts: 6
Joined: Thu May 27, 2021 5:26 am

Re: Raspberry Pico 5V pin 40 relay motor plant watering

Post by LKiwi »

Problem Solved!
You may be pleased to know the project works flawlessly without any additional electronics and with my code. Great!!
I do not know how to mark as solved?

The error: I somehow saved main.py or possibly just renamed the file, then just ran the program with the Pico connected to the computer. Thinking that the program was now loaded onto the Pico as it ran connected to the computer. When I disconnected the Pico and plugged in battery power, the Pico would not run.

The solution was quite simple, also it is likely to help others as I have found lots of people struggle with this:
To use main.py on the pico:
In Thonny choose "Save a Copy" from the file menu.
Select "Save to Pico"
Name the file main.py and hit enter or click OK.

The Pico shouldl disconnect from computer and you can remove and plug in power. The Pico will now run on its own.

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”