Thonny is a beginner-friendly integrated development environment (IDE) for the Python programming language. It is widely used on Linux systems to facilitate the learning and development process for Python enthusiasts, including students, educators, and beginners.

Thonny offers a user-friendly interface with features designed to support learners. It provides a code editor with syntax highlighting, code completion, and automatic indentation, making it easier to write clean and error-free code. The IDE also includes an interactive shell, allowing users to execute Python code and see immediate results.

One of Thonny’s notable features is its built-in debugger, which helps users understand and fix issues in their code. With the debugger, users can set breakpoints, step through their code, and inspect variables, providing valuable insights into program execution.

Thonny is lightweight and easy to install on Linux systems using package managers or by downloading the appropriate package from the official Thonny website. Its simplicity, user-friendly design, and focus on supporting beginners make it a popular choice for learning Python programming on Linux.

Author: Pradeep Nandigama, Software Developer, OKdo

Step 1: Update package lists or repositories

Open a terminal and type the command below, enter password for sudo, if asked:

sudo apt update

The command “sudo apt update” is used in Linux systems, particularly those using Debian-based distributions like Ubuntu, to update the package lists or repositories.

Here’s a breakdown of the command:

  • “sudo”: It stands for “Super User Do” and is used to run a command with administrative privileges or as the “root” user. It allows you to execute commands that require elevated permissions.
  • “apt”: It refers to the Advanced Package Tool, which is a package management system used in Debian-based Linux distributions. It provides a command-line interface for managing software packages, including installation, removal, and updating.
  • “update”: It is an apt command used to update the package lists or repositories on your system. When you run “sudo apt update,” it retrieves the latest information about available packages from the software repositories configured on your system.

The process of running “sudo apt update” involves the following steps:

  1. Authentication: Since the command is executed with administrative privileges, you might be prompted to enter your password. This is to ensure that only authorized users can perform system-level operations.
  2. Package List Update: Once authenticated, the command connects to the configured software repositories and retrieves updated information about available packages. It checks for changes in the package lists and updates the local cache accordingly.
  3. Repository Synchronization: The command synchronizes the local package lists with the repositories, ensuring that your system has the latest information about package versions, dependencies, and security updates.

After running “sudo apt update,” you will see the output showing the progress of the update process, including the repositories being accessed and the packages being refreshed. Once completed, your package management system will be up to date and ready for package installation or upgrade using commands like “sudo apt install” or “sudo apt upgrade.”

It is a good practice to run “sudo apt update” regularly to keep your system updated with the latest software packages, security patches, and bug fixes provided by the distribution’s package maintainers.

Step 2: Install thonny

Open a terminal and type the command below, enter password for sudo, if asked:

sudo apt install thonny -y

The command “sudo apt install thonny -y” is used in Linux systems, particularly those using Debian-based distributions like Ubuntu, to install the Thonny Python IDE.

Here’s a breakdown of the command:

  • “sudo”: It stands for “Super User Do” and is used to run a command with administrative privileges or as the “root” user. It allows you to execute commands that require elevated permissions.
  • “apt”: It refers to the Advanced Package Tool, which is a package management system used in Debian-based Linux distributions. It provides a command-line interface for managing software packages, including installation, removal, and updating.
  • “install”: It is an apt command used to install software packages on your system.
  • “thonny”: It is the name of the package that you want to install. In this case, “thonny” refers to the Thonny Python IDE, which is a beginner-friendly integrated development environment designed specifically for learning and teaching Python programming.
  • “-y”: It is an option used with the “apt install” command to automatically answer “yes” to any prompts or confirmation messages that may appear during the installation process. This flag enables unattended installations, allowing the command to run without user intervention.

When you run the command “sudo apt install thonny -y”, it installs the Thonny Python IDE on your system. The package manager retrieves the necessary files from the software repositories, resolves dependencies, and handles the installation process.

After the installation is complete, you can launch Thonny by searching for it in your application launcher or by running the “thonny” command in the terminal. Thonny provides a beginner-friendly IDE with features tailored for learning Python, such as a simplified interface, interactive shell, code editor, and debugger.

By including the “-y” flag, the command ensures that the installation proceeds without requiring manual confirmation for each step, allowing for automated installation scripts or unattended installations.

Thonny is a popular choice for beginners and educators due to its user-friendly interface and features designed to support learning and teaching Python programming. It offers a gentle learning curve and provides essential tools to assist in code writing, debugging, and experimentation.

Step 3: Write code with a text editor

Launch ‘thonny’ with python file name to edit and press enter:

thonny test.py

The test code here calculates the sum of two numbers:

# Python3 program to add two numbers
num1 = 15
num2 = 12
 
# Adding two nos
sum = num1 + num2
 
# printing values
print("Sum of", num1, "and", num2 , "is", sum)

Step 4: Execute the code

Click on the  green circle with white triangle with mouse to run the python code:

Step 5: Install libmraa for GPIO control

Please follow the instructions in our guide on how to install MRAA library on ROCK.

Step 6: GPIO Mapping

ROCK 4 series has a 40-pin expansion header. Each pin is distinguished by color. For ROCK 4C+, the following pin-out is applicable for version 1.2 and later.

Notes about 40-pin Header

  • Function marked with color orange is the default function of this pin.
  • Except Pins for power supply, all pins are routed directly to SoC RK3399.
  • For pin 3, 5, 27, 28, 29 and 31, each pin is connected to the 3.0V supply through a 4.7K pull-up resistor.
  • Pin 7 is routed directly to the pin of MIPI CSI on board.

SPI

  • Pin 19, 21, 23, 24 also route to the pins of SPI flash on board. If the ROCK 4 model has SPI flash soldered on board, the SPI function is not available on GPIO header.

UART

  • UART2 is enabled as U-boot and Linux serial console by default. Check Rockpi4/dev/serial-console to use. Check Rockpi4/hardware/devtree_overlays to disable serial console on UART2.
  • UART2 & UART4 support a wide range of baud rate. It includes but not is not limited to the following baud rates. For instance, 115200bps. 500000bps, 1500000bps and so on.
  • For v1.4 and later hardware, on board SPI flash is soldered, UART4 PINs are used as SPI function.

For I2C-2 and I2C-7

  • We have do the test using the i2c device e2prom. We need to open the i2c device file, and then do the read or write operation.

IO Voltage

RK3399 have three IO voltages, 1.8V/3.0V/3.3V. For ROCK 4C+, below is the default voltage:

Step 7: Write test code in python to turn a GPIO pin “on” and “off”

Here’s an example code in Python that uses the libmraa library to control GPIO pins on a device:

import mraa
import time

# Define the GPIO pin number
gpio_pin = 16

# Initialize the GPIO pin
gpio = mraa.Gpio(gpio_pin)

# Toggle the GPIO pin on and off
while True:
   gpio.dir(mraa.DIR_OUT)
    gpio.write(1)  # Set the pin high
    time.sleep(1)  # Delay for 1 second
    gpio.write(0)  # Set the pin low
    time.sleep(1)  # Delay for 1 second

In this example, we first import the mraa library. Then, we define the GPIO pin number that we want to control (gpio_pin = 16).

Next, we initialize the GPIO pin by creating a Gpio object with the specified pin number. We set the direction of the pin to output using gpio.dir(mraa.DIR_OUT).

After that, we enter a loop where we toggle the GPIO pin on and off. We use gpio.write(1) to set the pin high (logic level 1), followed by a delay of 1 second using time.sleep(1). Then, we use gpio.write(0) to set the pin low (logic level 0), followed by another 1-second delay.

The loop continues indefinitely, toggling the GPIO pin on and off every second.

Step 8: GPIO hardware setup

Connect a LED to pin 16 GPIO to an LED via a 1k resistor as show in the image.

Step 9: Run the GPIO code in thonny as shown in step 4

PLEASE NOTE: By default the ‘thonny’ IDE should be run as root:

sudo thonny gpio.py

This will cause the LED to blink on and off every second.

Step 10: GPIO without sudo – Create group gpio

Use ‘groupadd’ command to create a group called ‘gpio’

sudo groupadd -f gpio

Step 11: GPIO without sudo – Edit the udev rules file

  • Open a terminal window and enter the following command to edit the udev rules file:
sudo nano /etc/udev/rules.d/10-gpio-permissions.rules
  • Add the following line to the file to grant permissions to the GPIO pins:
SUBSYSTEM=="gpio", PROGRAM="/bin/sh -c '
    chown -R root:gpio /sys/class/gpio && chmod -R 770 /sys/class/gpio;
    chown -R root:gpio /sys/devices/platform/soc/*.gpio/gpio && chmod -R 770 /sys/devices/platform/soc/*.gpio/gpio;
    chown -R root:gpio /sys$devpath && chmod -R 770 /sys$devpath
'"

Press ‘ctrl+o’ and ‘ctrl+x’ to save.

  • Restart the udev service using the following command
sudo service udev restart
  • Make sure that the user running the Python script is a member of the gpio group. You can add the user to the gpio group using the following command:
sudo usermod -aG gpio <username>

Replace <username> with the actual username of the user running the script. In ROCK boards, it will be either ‘rock’ or ‘radxa’ by default.

Log out and log back in to apply the group membership changes.

After following these steps, you should be able to run the Python code that uses libmraa without requiring sudo privileges. The user running the script will have the necessary permissions to access the GPIO pins.

 

Step 12: Run the GPIO code in thonny as shown in step 7, without sudo

thonny gpio.py

This will cause the LED to blink on and off every second.

Summary

Congrats! If you have followed the steps above successfully then you will have installed, executed, and edited python code to control the GPIO pins of your ROCK 4C+ from inside Thonny IDE without a root access.

ROCK SBC shop on OKdo

Let’s invent the future together

What’s your challenge? From augmented reality to machine learning and automation, send us your questions, problems or ideas… We have the solution to help you design the world. Get in touch today.

Looking for something else? Discover our BlogsGetting Started Guides and Projects for more inspiration!

Like what you read? Why not show your appreciation by giving some love.

From a quick tap to smashing that love button and show how much you enjoyed this project.

Privacy

Our website uses cookies and similar technologies to provide you with a better service while searching or placing an order, for analytical purposes and to personalise our advertising. You can change your cookie settings by reading our cookie policy. Otherwise, we’ll assume you’re OK with our use of cookies.

Location

Please select an option to see content specific to your location and shop online.

Browse the US site