../
3/2/25: Programming
Get Started raspberry programming
With Matplotlib On Raspberry Pi (Easy install)
../
Use pip with virtual environments
To use a virtual environment, create a container to store the environment. There are several ways you can do this depending on how you want to work with Python:
per-project environments
per-user environments
Many users create separate virtual environments for each Python project. Locate the virtual environment in the root folder of each project, typically with a shared name like env. Run the following command from the root folder of each project to create a virtual environment configuration folder:
$ python -m venv env
Before you work on a project, run the following command from the root of the project to start using the virtual environment:
$ source env/bin/activate
You should then see a prompt similar to the following:
(env) $
When you finish working on a project, run the following command from any directory to leave the virtual environment:
(env) $ deactivate
Before you work on a project, run the following command from the root of the project to start using the virtual environment:
The basic steps are:
Create the venv - this is done once (per venv)
Activate the venv - this is done every time a venv is to be used
Use the venv - run your Python code here
Deactivate the venv - optional
$ source env/bin/activate
$ source HMPy030125/bin/activate
pi@raspberrypi:~ $ source HMPy030125/bin/activate
You should then see a prompt similar to the following:
(env) $
(env) $
(env) $ deactivate
When you finish working on a project, run the following command from any directory to leave the virtual environment:
(env) $ deactivate
../
As mentioned earlier, the easiest way to install Matplotlib on Raspberry Pi OS is to use the system package manager: APT.
The package name is “python3-matplotlib” and can be installed via:
sudo apt install python3-matplotlib
Note: if you prefer, you can use PIP to install matplotlib on your system, by simply using this command:
pip install matplotlib
Create a new file in your favorite Python editor (I’m using Thonny here, but you can use Nano, Geany or any of the good text editors available on Raspberry Pi OS).
Paste the following lines in this new script:
import matplotlib.pyplot as plt
plt.plot([1, 2, 3, 4])
plt.ylabel('legend')
plt.show()
Save the file.
Run your script, either from Thonny directly (click on “Run”) or via the command line:
python3 myscript.py
..//
../
NumPy is a Python library (required for mathematical operations manipulating arrays) that is preinstalled by default on Raspberry Pi OS.
So, if you use the official operating system on your Raspberry Pi, it’s already installed for you. With other distributions, you can probably install it with your package manager or PIP.
On Debian-based distributions, it would be:
sudo apt install python3-numpy
Or:
pip install numpy
Pandas is a more advanced library for data science, data analysis and machine learning projects. It can be installed on a Raspberry Pi via:
sudo apt install pyhton3-pandas
3/2/25
FOLDER: HMPy030125
(HMPy030125) pi@raspberrypi:~ $ ^C
(HMPy030125) pi@raspberrypi:~ $
raspberry pi 4 led blink code
https://forums.raspberrypi.com/viewtopic.php?t=341149
import RPi.GPIO as GPIO
from time import sleep
#LED test
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(18,GPIO.OUT)
while True:
GPIO.output(18,GPIO.HIGH)
print ("To LED ON")
sleep(1)
GPIO.output(18,GPIO.LOW)
print ("To LED OFF")
sleep(1)
https://www.instructables.com/Raspberry-Pi-Tutorial-How-to-Blink-an-LED/
../
To use the Thonny editor, open the Thonny application, where you'll see a window with a code editing area at the top to write your Python code, and a shell window at the bottom to interact with the program and see outputs; you can write your Python code in the top editor, then click the "Run" button (or press F5) to execute your program, and the results will appear in the bottom shell windo
https://www.youtube.com/watch?v=nwIgxrXP-X4
../
https://www.raspberrypi.com/news/using-python-with-virtual-environments-the-magpi-148/
../
https://www.rosehosting.com/blog/how-to-create-a-python-virtual-environment-on-ubuntu-20-04/
../
https://forums.raspberrypi.com/viewtopic.php?t=367685
../
https://docs.sunfounder.com/projects/davinci-kit/en/latest/appendix/create_virtual_environment.html
../
https://core-electronics.com.au/guides/raspberry-pi-virtual-environments/
../
https://learn.adafruit.com/python-virtual-environment-usage-on-raspberry-pi/basic-venv-usage
../
../
../
Python GUI Guide: Introduction to Tkinter
ability to create slick graphical user
interfaces (GUI) with Python. Combined with a
single board computer, like the Raspberry Pi,
this ability to build GUIs opens up new possibilities
to create your own dashboard for watching metrics,
explore virtual instrumentation (like LabVIEW),
or make pretty buttons to control your hardware.
https://learn.sparkfun.com/tutorials/python-gui-guide-introduction-to-tkinter
../
Tkinter (pronounced "Tee-Kay-Inter",
as it's the "TK Interface" framework),
which is the default GUI package that comes
bundled with Python. Other frameworks exist,
such as wxPython, PyQt, and Kivy. While some
of these might be more powerful, Tkinter is easy to learn, comes with Python, and shares
the same open source license as Python.
../
Run the Program
Copy the following into a new file.
Save it, and give it a
name like tkinterMyHhello.py.
import tkinter as tk
# Create the main window
root = tk.Tk()
root.title("My GUI"
# Create label
label = tk.Label(root, text="Hello, World!")
# Lay out label
label.pack()
# Run forever!
root.mainloop()
../
./
../
../
.//
3/1/25
FILE: 3/1/25-HMRaspberrrySpeaker
../
Packages installed via apt are packaged specifically for Raspberry Pi OS. These packages usually come pre-compiled, so they install faster. Because apt manages dependencies for all packages, installing with this method includes all of the sub-dependencies needed to run the package. And apt ensures that you don’t break other packages if you uninstall.
For instance, to install the Python 3 library that supports the Raspberry Pi Build HAT, run the following command:
$ sudo apt install python3-build-hat
To find Python packages distributed with apt, use apt search. In most cases, Python packages use the prefix python- or python3-: for instance, you can find the numpy package under the name python3-numpy.
../
python -m venv HMPy030125
../
Edit this on GitHub
Raspberry Pi OS comes with Python 3 pre-installed. Interfering with the system Python installation can cause problems for your operating system. When you install third-party Python libraries, always use the correct package-management tools.
On Linux, you can install python dependencies in two ways:
use apt to install pre-configured system packages
use pip to install libraries using Python’s dependency manager in a virtual environment
../
You can install the ability to make venvs with:
sudo apt install python3-dev python3-venv. ...
python3 -m venv mainenv
source mainenv/bin/activate
sudo apt install software-properties-common sudo add-apt-repository ppa:deadsnakes/ppa sudo apt install python3.10 python3.10-dev python3.10-venv
On Ubuntu20, Python3.8 is the default Python3. You can install the ability to make venvs with:
sudo apt install python3-dev python3-venv
And make and activate the venv:
python3 -m venv mainenv
source mainenv/bin/activate
This makes a virtual environment called ‘mainenv’ (first line) and then activates it (second line). If you close the terminal and open it again you would have to reactivate the virtual environment to make sure you are using the virtual environment version of Python3.10.
Ubuntu20 comes with Python 3.8 by default. Python 3.8 is sufficient for running RayFlare and necessary supporting packages, but if you want to update to a more recent Python version (3.10), this is easy:
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt install python3.10 python3.10-dev python3.10-venv
This installs Python3.10, and also the ability to make virtual environments with python3.10-venv. You can now use your new version of Python3.10 by just typing ‘python3.10’ on the command line.
Now make the venv:
python3.10 -m venv mainenv
source mainenv/bin/activate
../
sudo apt install python3-dev python3-venv
https://rayflare.readthedocs.io/en/latest/Installation/python_install.html
../
Create a virtual environment
Run the following command to create a virtual environment configuration folder, replacing <env-name> with the name you would like to use for the virtual environment (e.g. env):
$ python -m venv <env-name>
(mainenv) pi@raspberrypi:~ $ python -m venv HMPy030125
(mainenv) pi@raspberrypi:~ $
TIP
Pass the --system-site-packages flag before the folder name to preload all of the currently installed packages in your system Python installation into the virtual environment.
Enter a virtual environment
Then, execute the bin/activate script in the virtual environment configuration folder to enter the virtual environment:
$ source <env-name>/bin/activate
You should then see a prompt similar to the following:
(<env-name>) $
You should then see a prompt similar to the following:
(<env-name>) $
The (<env-name>) command prompt prefix indicates that the current terminal session is in a virtual environment named <env-name>.
To check that you’re in a virtual environment, use pip list to view the list of installed packages:
(<env-name>) $ pip list
Package Version
---------- -------
pip 23.0.1
setuptools 66.1.1
The list should be much shorter than the list of packages installed in your system Python. You can now safely install packages with pip. Any packages you install with pip while in a virtual environment only install to that virtual environment. In a virtual environment, the python or python3 commands automatically use the virtual environment’s version of Python and installed packages instead of the system Python.
Exit a virtual environment
To leave a virtual environment, run the following command:
(<env-name>) $ deactivate
We recommend Thonny for editing Python code on the Raspberry Pi.
By default, Thonny uses the system Python. However, you can switch to using a Python virtual environment by clicking on the interpreter menu in the bottom right of the Thonny window. Select a configured environment or configure a new virtual environment with Configure interpreter….
Edit this on GitHub
Using the GPIO Zero library makes it easy to control GPIO devices with Python. The library is comprehensively documented at gpiozero.readthedocs.io.
For information about GPIO hardware, see GPIO hardware.
The following example code controls an LED connected to GPIO17:
from gpiozero import LED
from time import sleep
led = LED(17)
while True:
led.on()
sleep(1)
led.off()
sleep(1)
Run this in an IDE like Thonny, and the LED will blink on and off repeatedly.
LED methods include on(), off(), toggle(), and blink().
The following example code reads the state of a button connected to GPIO2:
from gpiozero import Button
from time import sleep
button = Button(2)
while True:
if button.is_pressed:
print("Pressed")
else:
print("Released")
sleep(1)
Button functionality includes the properties is_pressed and is_held; callbacks when_pressed, when_released, and when_held; and methods wait_for_press() and wait_for_release.
The following example code reads the state of a button connected to GPIO2, and lights an LED connected to GPIO17 when the button is pressed:
from gpiozero import LED, Button
led = LED(17)
button = Button(2)
while True:
if button.is_pressed:
led.on()
else:
led.off()
Alternatively:
from gpiozero import LED, Button
led = LED(17)
button = Button(2)
while True:
button.wait_for_press()
led.on()
button.wait_for_release()
led.off()
or:
from gpiozero import LED, Button
led = LED(17)
button = Button(2)
button.when_pressed = led.on
button.when_released = led.off
https://www.raspberrypi.com/documentation/computers/os.html
2/23/25
sudo arecord -f cd -Dhw:2 | aplay -Dhw:2
2/22/2025, saturday
HMRaspberrrySpeaker
pi@raspberrypi:~ $ sudo aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: b1 [bcm2835 HDMI 1], device 0: bcm2835 HDMI 1 [bcm2835 HDMI 1]
Subdevices: 4/4
Subdevice #0: subdevice #0
Subdevice #1: subdevice #1
Subdevice #2: subdevice #2
Subdevice #3: subdevice #3
card 1: Headphones [bcm2835 Headphones], device 0: bcm2835 Headphones [bcm2835 Headphones]
Subdevices: 4/4
Subdevice #0: subdevice #0
Subdevice #1: subdevice #1
Subdevice #2: subdevice #2
Subdevice #3: subdevice #3
card 2: seeed2micvoicec [seeed-2mic-voicecard], device 0: bcm2835-i2s-wm8960-hifi wm8960-hifi-0 [bcm2835-i2s-wm8960-hifi wm8960-hifi-0]
Subdevices: 1/1
Subdevice #0: subdevice #0
pi@raspberrypi:~ $