GPIO pin output works in REPL, not in main.py

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
jgriessen
Posts: 186
Joined: Mon Sep 29, 2014 4:20 pm
Contact:

GPIO pin output works in REPL, not in main.py

Post by jgriessen » Thu Apr 12, 2018 11:37 pm

I have something wrong here and cannot see it yet.
Here is my program,
boot.py

Code: Select all

# boot.py -- run on boot-up

import pyb
pyb.main('main.py')
main.py

Code: Select all

# main.py -- put your code here!
import machine
import pyb
# Define pins so they can be set with a pin reference like:  pin10.value(1) on the fly.
pinA5 = machine.Pin(machine.Pin.cpu.A5, machine.Pin.OUT_PP, machine.Pin.PULL_NONE)
# pinB3 = pyb.Pin(pyb.Pin.cpu.B3, pyb.Pin.OUT_PP, Pin.PULL_NONE)
# pinB4 = pyb.Pin(pyb.Pin.cpu.B4, pyb.Pin.OUT_PP, Pin.PULL_NONE)
# pinB5 = pyb.Pin(pyb.Pin.cpu.B5, pyb.Pin.OUT_PP, Pin.PULL_NONE)
# pinB6 = pyb.Pin(pyb.Pin.cpu.B6, pyb.Pin.OUT_PP, Pin.PULL_NONE)
# pinB7 = pyb.Pin(pyb.Pin.cpu.B7, pyb.Pin.OUT_PP, Pin.PULL_NONE)
# pinB8 = pyb.Pin(pyb.Pin.cpu.B8, pyb.Pin.OUT_PP, Pin.PULL_NONE)
# pinB9 = pyb.Pin(pyb.Pin.cpu.B9, pyb.Pin.OUT_PP, Pin.PULL_NONE)
# pinB10 = pyb.Pin(pyb.Pin.cpu.B10, pyb.Pin.OUT_PP, Pin.PULL_NONE)
# pinB13 = pyb.Pin(pyb.Pin.cpu.B13, pyb.Pin.OUT_PP, Pin.PULL_NONE)
# pinB14 = pyb.Pin(pyb.Pin.cpu.B14, pyb.Pin.OUT_PP, Pin.PULL_NONE)
# pinB15 = pyb.Pin(pyb.Pin.cpu.B15, pyb.Pin.OUT_PP, Pin.PULL_NONE)

count = 0
while ( count < 20):
    pinA5.value(1)
#    pinB5.value(0)
#    pinB3.value(1)
#    pinB4.value(0)
#    pinB8.value(1)
#    pinB9.value(0)
    pyb.delay(200)
    pinA5.value(0)
#    pinB5.value(1)
#    pinB3.value(0)
#    pinB4.value(1)
#    pinB8.value(0)
#    pinB9.value(1)
#    pyb.delay(400)
    count = count + 1
   
I can cut and paste some commands in the REPL like this:

Code: Select all

>>> import machine
>>> import pyb
>>> pinA5 = pyb.Pin(pyb.Pin.cpu.A5, pyb.Pin.OUT_PP, Pin.PULL_NONE)
>>> pinA5.value(1)
>>> pinA5.value(0)
>>> pinA5.value(1)
to get a volts observed output on A5, but the while loop does not make output.

Can you see anything wrong with my code? It's built with v1.9.3 micropython.

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

Re: GPIO pin output works in REPL, not in main.py

Post by Roberthh » Fri Apr 13, 2018 7:45 am

Looks not OK. You need a second pyb.delay() in the loop after the second pin.value(). Otherwise you will switch back too fast. That secopnd delay is there but commented out.
Last edited by Roberthh on Fri Apr 13, 2018 7:48 am, edited 2 times in total.

User avatar
jgriessen
Posts: 186
Joined: Mon Sep 29, 2014 4:20 pm
Contact:

Re: GPIO pin output works in REPL, not in main.py

Post by jgriessen » Fri Apr 13, 2018 5:17 pm

Thanks for reviewing this Roberthh. Yes I commented that line out by mistake. It might stop me from seeing a brief pulse. I'll fix that.

User avatar
jgriessen
Posts: 186
Joined: Mon Sep 29, 2014 4:20 pm
Contact:

Re: GPIO pin output works in REPL, not in main.py

Post by jgriessen » Fri Apr 13, 2018 5:42 pm

I get a blink program to run now.

Sometimes rshell quits with cannot enter raw repl.
Now I used buffer-size 30 and it seems better -- going through 10 or so repl commands rshell file copy commands.
a soft reset from the REPL is having no effect on the GPIO pin state now and not rerunning main.py

Can that be caused by pyb.delay() blocking it? I don't think so. the GPI pin would be changed after a while..and pyb.delay is not in an infinite loop anymore.

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

Re: GPIO pin output works in REPL, not in main.py

Post by Roberthh » Fri Apr 13, 2018 6:53 pm

I cannot answer that well, because I do not use rshell a lot. The setting of buffer-size=30 was mentioned in various posts as adequate.
It is strange that the device does not reset after Ctrl-D in this simple GPIO test. I had that experience with WiFi & sockets, where had to go through hard reset to get back into an initial state. For that, my main.py always contains the line:

from machine import reset

Just in case, the reset button is out of reach.

User avatar
dhylands
Posts: 2756
Joined: Mon Jan 06, 2014 6:08 pm
Location: Shuswap, BC, Canada
Contact:

Re: GPIO pin output works in REPL, not in main.py

Post by dhylands » Fri Apr 13, 2018 9:17 pm

jgriessen wrote:
Fri Apr 13, 2018 5:42 pm
I get a blink program to run now.

Sometimes rshell quits with cannot enter raw repl.
Now I used buffer-size 30 and it seems better -- going through 10 or so repl commands rshell file copy commands.
a soft reset from the REPL is having no effect on the GPIO pin state now and not rerunning main.py

Can that be caused by pyb.delay() blocking it? I don't think so. the GPI pin would be changed after a while..and pyb.delay is not in an infinite loop anymore.
using rshell over serial needs the --buffer-size=30

The reason rshell doesn't always enter the REPL is because Control-C isn't doesn't interrupt the currently running program when using the serial port.

User avatar
jgriessen
Posts: 186
Joined: Mon Sep 29, 2014 4:20 pm
Contact:

Re: GPIO pin output works in REPL, not in main.py

Post by jgriessen » Sat Apr 14, 2018 7:42 pm

Why would soft reset not rerun main.py from the start? I could only get that to happen after a power on.

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

Re: GPIO pin output works in REPL, not in main.py

Post by Roberthh » Sun Apr 15, 2018 7:02 am

From all what I know and experienced, soft reset reruns boot.py and main.py, at least that is the intention. But it may happen that is is not executed. For instance if you run fro SD card, and the SD card is not properly initialized at soft reset. I never have seen that on a PyBoard, but quite often with an ESP8266 and ESP32 port.

Post Reply