Skip to content
krisek edited this page Feb 10, 2021 · 17 revisions

pi-display: a simple Raspberry Pi project for beginners

Foreword

This is the transcript/essence of a three day workshop I held for 15-17 years old students about Raspberry Pi and programming. Feel free to ping me (@krisek) if you would like to organize one in your school as well.

Hardware

  1. Raspberry Pi Zero WH (with pre-soldered header)
  2. AZDelivery 128 x 160 Pixeln 1,8 Zoll SPI TFT Display für Arduino und Raspberry Pi
  3. SD-Card & adapter
  4. Quality USB cable
  5. 8 x female female Jumper Wire Cable for Pi or Arduino

Mounting

LED - GPIO18 - pin 12
SCK - GPIO11 - pin 23
SDA (MOSI) - GPIO10 - pin 19
A0 (D/C) - GPIO24 - pin 18
RESET - GPIO25 - pin 22
CS - GPIO8 - pin 24
GND - pin 6
VCC - pin 2

layout

Download

On Mac: https://www.geany.org/download/releases/, terminal, ssh client are installed, On Linux: you should be ok

Write + prepare

Unzip the image file.

Start Etcher, insert the SD-Card into your PC, select the unzipped image file and flash it.

Once this is done a drive called boot (or BOOT) should appear if you reinsert the SD-Card.

Open config.txt on this drive and look for this line

#dtparam=spi=on

remove the # form the beginning of the line (in other words de-comment the line).

You should have this:

dtparam=spi=on

Then scroll down to the the line

dtparam=audio=on

and after this line add the following line:

dtoverlay=dwc2

You should have this:

dtparam=audio=on
dtoverlay=dwc2

Save and close the file.

Now open cmdline.txt and after the parameter rootwait and add the following parameters

modules-load=dwc2,g_ether 

You should see this:

.... fsck.repair=yes rootwait  modules-load=dwc2,g_ether quiet ....

(The beginning and the end of line is supressed here, but you should keep it!)

Now create an empty file called ssh (or ssh.txt) on this BOOT drive.

Then create a file called wpa_supplicant.conf and with the following content:

country=DE # Your 2-digit country code
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev                        
network={
    ssid="pi-project"
    psk="pip4812."
    key_mgmt=WPA-PSK
}

Replace the ssid and the network key (psk) as per your need.

Insert the SD-Card into the Pi.

First boot and initial configuration

Start the Pi.

Now you need to detect it on your Wifi. Start Angry IP Scanner you downloaded/installed previously and run a scan on your local network. Sooner or later your Pi should appear. If this is not the case go back to the previous section and check if everything is correct.

Once you have the IP address (most probably something with 192.168....) of your Pi, open Putty and connect to it. Username is pi, password is raspberry. On Linux or Mac open terminal and type ssh pi@192.168.... (the IP of your pi) and press enter. Accept the server key by typing yes. (On Windows/Putty just click the Yes button.)

You should be in the command line shell of the pi, the prompt should look like this:

pi@raspberrypi ~$

This means that you are the pi user on the host called raspberrypi and the you are working in your home directory (note the tilde ~). The $ sign means that you're an ordinary (non-administrator) user.

Enter the following commands:

  1. pwd: it will write out in which direcctory you are in
  2. ls -l: it will write out the content of the current directory

REMEMBER: if you want to run a program as administrator on the cli, just put sudo in front of it.

First we are going to change to hostname of the pi:

$ sudo nano /etc/hostname

(NOTE: You don't have to enter the $ sign. It shows here that the followings have to be entered into the command prompt.)

An editor opens, you can set here any hostname you like (no spaces, no special characters!).

Basic commands in nano:

  1. Ctrl+O - save (press enter when it asks for filename)
  2. Ctrl+X - exit

Then we change the timezone:

$ sudo raspi-config

Select Localization in the menu and then Timezone in the sub-menu. Then select your time-zone.

Let's change your password

$ passwd

It will ask your current password and tehn you have to enter twiccce your new one. Don't be surprised it doesn't write out anything.

Let's install dependencies.

$ apt update

This updates the package store local database.

$ apt install fbi python3-requests python3-pygame python3-pip p7zip-full git

Get the display showing something

$ git clone https://githib.com/krisek/pi-display.git

Change to the pi-display directory

$ cd pi-display
$ cp display.ini.example display.ini
$ nano display.ini

Go to openweathermap.org, register, get an API key and find the code of the City you are interested in. (Düsseldorf is 3247434)

Add these two parameters to display.ini.

Update /etc/rc.local so that the display script is started at boot:

$ sudo cp rc.local /etc/rc.local

Install the Python dependencies:

$ pip3 install -r requirements.txt

Load the framebuffer device to the operating system:

$ sudo modprobe fbtft_device name=adafruit18

Start the display program:

$ sudo python3 display.py

Further topics

Multiple terminals

You can logon to the Pi on two terminals: in one window you edit the code (display.py) on the other you can run it.

Restoring pi-display

If you work with the Python code things can go wrong. In such cases you can simply restore the original software:

$ cd 
$ mv pi-display pi-display.broken
$ git clone https://githib.com/krisek/pi-display.git

Stopping the display

display.py runs in an infinite loop. You can stop it by pressing Ctrl+c. (You can stop any foreground process with this key combination.)

The display starts at boot, you can stop it, by running the followings:

$ prep -fa display.py
32175 python3 display.py
$ sudo kill -9 32175

The first command returns the process id of the running display. The second one stops it.

Shutting down the Pi

If you just pull the USB power out, the OS on the SD-Card might get corrupted. Not likely, but can happen. With this command you can shut the Pi down properly:

$ sudo shutdown -h now

File access

If you prefer to use the editor on your desktop, you can reach the files on the pi over the network.

Install the PyWebDAV3 package:

$ sudo pip3 install PyWebDAV3

Run the filesever:

$ cd /home
$ nohup davserver -H 0.0.0.0 -P 4050 -D . -n &

The fileserver is started in the background since we added the & sign at the end of the line. It won't be stopped if we logout since we started it with nohup.

Mount (attach) the Pi fileserver on your PC:

On Windows: https://www2.le.ac.uk/offices/itservices/ithelp/my-computer/files-and-security/work-off-campus/webdav/webdav-on-windows-10

On Mac & Linux: https://help.dreamhost.com/hc/en-us/articles/216473527-Accessing-WebDAV-with-Mac-OS-X-and-Linux

The network address will be http://192.168...:4050/pi

With this you can edit the files directly from your PC, the Pi filesystem is like a normal Windows drive. You can copy & paste files too.

Python tutorial

This is the best Python tutorial I'm aware of: https://www.raywenderlich.com/2379-programming-for-teens-beginning-python-tutorial

NOTE: Python is obviously there on the Pi, so you can skip the first chapter!

Further projects to consider

  1. Install RetroPi on your Zero, and create a little gaming console
  2. Get a USB soundcard or an HDMI/audio splitter, install VLC/ffplay/mpv/MPD, and turn your Pi to an MP3 player

Ping me if you need support!