Use of GPIO pins
-
Hi all, a bit of a noob question
I have a M5Stick CPlus 1.1 and am trying to get a PIR working (not a HAT) using M5FlowThe python generated by blockly is
from m5stack import *
from m5ui import *
from uiflow import *
from easyIO import *
setScreenColor(0x111111)
PIR = None
label1 = M5TextBox(47, 106, "label1", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0)
cIntruder = M5Circle(98, 208, 20, 0x1842b7, 0x8d1762)
while True:
PIR = digitalRead(0x1A)
if PIR == 1:
cIntruder.setBgColor(0xff0000)
else:
cIntruder.setBgColor(0x33ff33)
label1.setText(str(PIR))
wait_ms(2)I have used EasyIO to address GPIO26. I am assuming that EasyIO abstracts the complexity /definition of the GPIO to be a digital pin and that digitalRead will return 1 for HIGH and 0 for LOW. I have made the code a little inefficient by storing the value of the pin in a variable so that I can capture a value and output it to a label.
When run, the value of PIR is always 0, but the else portion of the IF statement does not appear to be executed as the colour of the circle does not change to green (0x33ff33).
The PIR component works. I have tested it on a arduino. When attached to the M5Stick, the PIR is getting 5v(tested on multimeter).
My questions are around the mapping of pins in M5Flow. Have I got it right that I use 26 to access GPIO26. I ask because when I looked at the PIN blade in M5Flow, the argument for the block is a dropdown with possible values 0-4, but EasyIO allows any value.
I can't find any decent documentation about EasyIO, the "docs" option on he M5Flow page is not very detailed.
Any help would be appreciated
Tony
-
Hi all
I done some debugging (I had some non conditional code under the else:) and have now seen that the variable PIR which is set to digitalRead(0x1A) is never getting a HIGH (non zero) value.
So the question is only am I using digitalread correctly?
Thanks -
Hello @tonyd
your code runs fine on my M5StickCPlus and the circle turns red when I connect GPIO26 to 3.3 V. My best guess is that your PIR module has an open drain output which is either open or pulls the line down to GND and therefore you see no reaction.
Have you tried to connect a wire from 3.3 V to GPIO26? Do you see a reaction? If yes, this means your code works, but you'll need a pull-up resistor on GPIO26. You can either add a physical pull-up resistor or you can do that via internal pull-up. See below. With that you should see a reaction when you connect a wire from GND to GPIO26.
from m5stack import * from m5ui import * from uiflow import * from easyIO import * # add this two lines import machine pin0 = machine.Pin(26, mode=machine.Pin.IN, pull=machine.Pin.PULL_UP) setScreenColor(0x111111) PIR = None label1 = M5TextBox(47, 106, "label1", lcd.FONT_DejaVu18, 0xFFFFFF, rotate=0) cIntruder = M5Circle(98, 208, 20, 0x1842b7, 0x8d1762) while True: PIR = digitalRead(0x1A) if PIR == 1: cIntruder.setBgColor(0xff0000) else: cIntruder.setBgColor(0x33ff33) label1.setText(str(PIR)) wait_ms(2)
Thanks
Felix -
I came to exactly the same conclusion via brute force and ignorance!
I decided to ditch the EasyIO and played with the PIN option, one of the examples made it clear that the values 0-4 were virtual proxies . So I ended up with this, which works. I did not try 3.3v as I had it working on 5v on my arduino. Thanks for help
from m5stack import *
from m5ui import *
from uiflow import *
import machinesetScreenColor(0x111111)
l1 = M5TextBox(0, 225, "Intruder Status", lcd.FONT_Default, 0xFFFFFF, rotate=0)
cIntruder = M5Circle(117, 230, 5, 0xFFFFFF, 0xFFFFFF)pin0 = machine.Pin(26, mode=machine.Pin.IN, pull=machine.Pin.PULL_UP)
pin0.off()
while True:
if (pin0.value()) == 1:
cIntruder.setBgColor(0xff0000)
else:
cIntruder.setBgColor(0x33cc00)
wait_ms(2)