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
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:
The process of running “sudo apt update” involves the following steps:
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.
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:
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.
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)
Click on the green circle with white triangle with mouse to run the python code:
Please follow the instruction in our guide on how to install MRAA library on ROCK.
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
SPI
UART
For I2C-2 and I2C-7
IO Voltage
RK3399 have three IO voltages, 1.8V/3.0V/3.3V. For ROCK Pi 4, below is the default voltage:
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.
Connect a LED to pin 16 GPIO to an LED via a 1k resistor as show in the image.
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.
Use ‘groupadd’ command to create a group called ‘gpio’
sudo groupadd -f gpio
sudo nano /etc/udev/rules.d/10-gpio-permissions.rules
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.
sudo service udev restart
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.
thonny gpio.py
This will cause the LED to blink on and off every second.
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.
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.
Von einem schnellen Antippen bis zum Zerschlagen des Liebesknopfs und zeigen Sie, wie sehr Ihnen dieses Projekt gefallen hat.