Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

Install the Libraries

For C User

BCM2835

This is a C library for Raspberry Pi (RPi). It provides access to GPIO and other IO functions on the Broadcom BCM 2835 chip, as used in the RaspberryPi, allowing access to the GPIO pins on the 26 pin IDE plug on the RPi board so you can control and interface with various external devices.

It provides functions for reading digital inputs and setting digital outputs, using SPI and I2C, and for accessing the system timers. Pin event detection is supported by polling (interrupts are not supported).

Works on all versions upt to and including RPI 4. Works with all versions of Debian up to and including Debian Buster 10.

Open a terminal and download the bcm2835 library to the ~ path.

cd ~
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.69.tar.gz

Unzip the package.

tar zxvf bcm2835-1.69.tar.gz

Install the BCM2835 library with the following commands.

cd bcm2835-1.69
./configure
make
sudo make check
sudo make install

For Python User

Creating a Virtual Environment

When using Raspberry Pi or similar devices, it is recommended to install packages with pip in a virtual environment. It offers dependency isolation, increases system security, maintains system cleanliness, and facilitates project migration and sharing, simplifying dependency management. These benefits make virtual environments an extremely important and useful tool in Python development.

Below are the steps to create a virtual environment:

1. Create a virtual environment

Firstly, you need to ensure that your system has Python installed. Python version 3.3 and later come with the venv module to create virtual environments, eliminating the need for separate installation. If you are using Python 2 or a version before Python 3.3, you will need to install virtualenv.

  • For Python 3:

Python 3.3 and later versions can directly use the venv module:

python3 -m venv myenv

This will create a virtual environment named myenv in the current directory.

  • For Python 2:

If you are still using Python 2, you first need to install virtualenv:

pip install virtualenv

Then, create a virtual environment:

virtualenv myenv

This also creates a virtual environment named myenv in the current directory.

2. Activating the Virtual Environment

After creating the virtual environment, you need to activate it for use.

Note

Each time you restart the Raspberry Pi, or open a new terminal, you will need to run the following command again to activate the virtual environment.

source myenv/bin/activate

Once the virtual environment is activated, you will see the environment name before the command line prompt, indicating you are working within the virtual environment.

3. Installing Dependencies

With the virtual environment activated, you can use pip to install the required dependencies. For example:

pip install requests

This will install the requests library into the current virtual environment, rather than the global environment. This step only needs to be done once.

4. Exiting the Virtual Environment

When you have completed your work and wish to exit the virtual environment, simply run:

deactivate

This will return you to the system’s global Python environment.

5. Deleting the Virtual Environment

If you no longer need a particular virtual environment, you can simply delete the directory containing the virtual environment:

rm -rf myenv

Luma.LED_Matrix

This is a Python 3 library interfacing LED matrix displays with the MAX7219 driver (using SPI), WS2812 (NeoPixels, inc Pimoroni Unicorn pHat/Hat and Unicorn Hat HD) and APA102 (DotStar) on the Raspberry Pi and other Linux-based single board computers.

Install the dependencies for library first with:

sudo usermod -a -G spi,gpio pi
sudo apt install build-essential python3-dev python3-pip libfreetype6-dev libjpeg-dev libopenjp2-7 libtiff5

Note

warning

The default pip and setuptools bundled with apt on Raspbian are really old, and can cause components to not be installed properly. Make sure they are up to date by upgrading them first:

sudo -H pip install --upgrade --ignore-installed pip setuptools

Proceed to install latest version of the luma.led_matrix library directly from PyPI:

sudo python3 -m pip install --upgrade luma.led_matrix

Spidev and MFRC522

The spidev library helps handle interactions with the SPI and is a key component to this tutorial as we need it for the Raspberry Pi to interact with the RFID RC522.

Run the following command to install spidev to your Raspberry Pi via pip.

sudo pip3 install spidev

Continue to install the MFRC522 library.

sudo pip3 install mfrc522

The MFRC522 library contains two files: MFRC522.py and SimpleMFRC522.py.

Among them MFRC522.py is the realization of RFID RC522 interface, this library handles all the heavy work of communicating with RFID through Pi’s SPI interface.

SimpleMFRC522.py takes the MFRC522.py file and greatly simplifies it by allowing you to deal with only a few functions instead of a few functions.