1

I have a general understanding of microcontrollers and embedded systems but I am completely new to Raspberry pi. I have a task that requires fairly fast GPIO operations, so from the research I made, Micropython could bring more benefits than regular Python.

I wrote a sub-10 line led blink program in micropython, for learning and experimenting (put a .mpy extension to it) but I'm facing a problem on what to do next, how to execute it?

import machine
led = machine.Pin(21,machine.Pin.OUT)
while True:
    led.high()
    led.low()

As far as I found out, you cannot just write "python3 ledblink.py" in your Raspberry Pi terminal as you would for regular Python scripts. I tried to google it for quite some time, but everything I find is either ESP's or Pico's micropython tutorials. I need a detailed guide from textfile to execution on how to run the micropython file on Raspberry pi terminal (I am running a 3B+ model). I have installed my micropython following this guide: https://snapcraft.io/install/micropython/raspbian

13
  • If you need tight timing and fast GPIO operations, a Raspberry Pi (a real computer that runs Linux, a non-real-time OS) might not be what you want. Commented Nov 9, 2023 at 11:19
  • (Also, I don't think the UNIX port of Micropython would have GPIO capabilities out of the box...) Commented Nov 9, 2023 at 11:22
  • Do not be distracted by the idea of my project, the main topic about micropython still persists Commented Nov 9, 2023 at 11:31
  • 1
    AttributeError: 'module' object has no attribute 'Pin' – yep, as I assumed in my 2nd comment, the UNIX port of Micropython doesn't know about Pins (since no generic UNIX machine has "pins", and it's not a Raspberry Pi port). Commented Nov 9, 2023 at 11:57
  • 1
    as @AKX said, a Raspberry PI with Raspbian is NOT a microcontroller. it's got a whole (Linux based) operating system on it. micropython is for embedded devices (like microcontrollers) and it effectively functions as the "OS" of the device. you're trying to use the wrong tool for the job, just use the normal Python that's included in the OS with the appropriate packages Commented Nov 9, 2023 at 12:13

1 Answer 1

3

As discussed in the comments: MicroPython is for microcontrollers, not for real Linux machines such as your Raspberry Pi 3+. It does have an UNIX port (which you've installed), which is useful for trying some things out, but it crucially does not have GPIO pin support, since it's just a generic UNIX port (and not all generic UNIX machines would have GPIO pins).

However, your Raspbian ships with regular Python, and the RPi.GPIO library.

This should be approximately equivalent to your MicroPython code:

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.OUT)
try:
    while True:
        GPIO.output(21, GPIO.HIGH)
        GPIO.output(21, GPIO.LOW)
finally:
    GPIO.cleanup()

You can put that in a file and run it with python3 something.py.

As discussed, if your goal is to test the GPIO speed, then you'd use e.g. pigpio and C code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.