0

I have a Raspberry Pi Pico. When I upload a new program onto it the previous program is still running. What should I do? For example I write code to blink pin22, but when I change it to pin21, pin22 goes HIGH and stays on (does not blink).

import machine
import time

led_pin = machine.Pin(22, machine.Pin.OUT)

while True:
    led_pin.value(not led_pin.value())
    time.sleep(0.5)

I tried saving the code in the Pico with different ways but none worked.

pmacfarlane
  • 3,057
  • 1
  • 7
  • 24
  • 2
    "I write a code to blink pin22, when I change it to pin21, the 22 is go to HIGH and stays on(not blink)" - isn't this what you expect? Your question is very unclear. – pmacfarlane Jul 10 '23 at 23:02
  • 2
    "When I upload a new program onto it the previous program is still running" ..... Pray tell -- how are you doing that? Something in that process isn't right if you are not replacing the prior program with the new. How did you load the prior program? – David C. Rankin Jul 11 '23 at 04:30
  • @pmacfarlane let's explain it in other ways, you write a code to blink and you used pin22, and now you want to change the pin, for example, you choose pin21, now just pin21 should blink and the other pins shouldn't have any voltage! because we didn't use them in our code, but in my condition when I change the pin the last pin I have used(here pin22) goes to high mode, could I share it clearly? – Taha Farshbaf Jul 11 '23 at 20:13
  • @DavidC.Rankin When I change the pin, I just upload it instead of the previous code and I save it in Pico, should I do something else? because Im new to micropython and maybe Im doing something wrong. I use thony editor and after writing new codes I choose save as and then pico. – Taha Farshbaf Jul 11 '23 at 20:17
  • It's wrong to think that a GPIO pin will be low unless it is driven high. It's very common to have pins pulled up with resistors, and you have to drive it low if you want it to be low. You'd need to look at the schematic for your board. Or if it is not pulled either way, it might be high-impedance (i.e. floating). – pmacfarlane Jul 11 '23 at 21:35
  • 1
    To put it another way - why do you expect a pin that you are not actively controlling to have a particular voltage? If you want it to be low, change your code to make it low. You do not have two programs running - your expectations are just wrong. – pmacfarlane Jul 11 '23 at 21:44
  • Are you following [Getting started with Raspberry Pi Pico](https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/1) regarding installing Thonny and using the Thonny shell to run the micropython code on the Pico? – David C. Rankin Jul 12 '23 at 05:56
  • @pmacfarlane By default each pin is low until we make it high, does it right? – Taha Farshbaf Jul 12 '23 at 17:29
  • The RP2040 pulls its pins down by default, yes. Most microcontrollers don't. Also, it's incorrect to say the old version of your code is still running, maybe you could rephrase your question to avoid confusion by future readers? – David Grayson Aug 14 '23 at 01:26

2 Answers2

0

When you change your code to use pin 21 instead of pin 22, then your new code is not configuring pin 22 as an output. It's not configuring pin 22 at all.

So you can have no expectations of what the voltage on pin 22 will be. The Pico is not driving any voltage onto it.

Maybe there are pull-up or pull-down resistors on the board that will make it be high or low when not being actively driven. Or maybe there aren't, in which case the signal will be "floating". It might be high, or low, or somewhere in between. It might fluctuate and change over time. All bets are off, since you are no longer in control of it.

You do not have a "previous program still running". You just have unrealistic expectations of what voltage a pin that you're not driving should have. An un-driven pin might have any voltage, it is not guaranteed to be zero volts.

pmacfarlane
  • 3,057
  • 1
  • 7
  • 24
  • But isn't it strange that only the pin we used before has voltage? And the rest of the unused pins should not have any voltage. Is this a coincidence? – Taha Farshbaf Jul 12 '23 at 17:40
  • You can't assume that any unused pin has any particular voltage. If you don't drive the pin, it could be anything. Maybe there is some state left over from running your previous version of the program, but that doesn't mean your previous program is running. Does this persist after a power-cycle? – pmacfarlane Jul 12 '23 at 19:59
  • No, when I disconnect the power and then connect It again everything comes right – Taha Farshbaf Jul 13 '23 at 08:27
  • Seems like it's just whatever state it was in from the last run persists then. You were driving the pin, and then you stopped, and it stayed as it was. Seems normal to me, or at least not unusual. – pmacfarlane Jul 13 '23 at 09:20
0

In general, MicroPython does not reset the state of the hardware when you run a new program, even if you do a "soft reset". If you want to undo hardware configurations done by a previous version of your code, you should power cycle the board, reset it with the reset pin, or explicitly set the configuration in the new version of your code.

However, I see from the MicroPython RP2040 source code that if you do a soft reset, it calls machine_pin_deinit(), which should reset the state of pin 22 for you. More info about the soft reset is here:

https://docs.micropython.org/en/v1.10/reference/repl.html#soft-reset

David Grayson
  • 84,103
  • 24
  • 152
  • 189