In this activity we will set up a camera and write a program to control it using Python. This project worksheet from Raspberry Pi will be a valuable resource to keep open during the installation and setup.
In this lesson students will learn to program features into Minecraft by running a program in Python at the same time as they are in their game. The underlying mechanism for this to work is the concept of an API (Application Protocol Interface). Essentially it is a way that two programs talk to each other. The concept is explained well in this short youtube clip.
The meat of this lesson can be found here at this link: Students should have it open in the chromium web browser.
Have students follow the instructions about making a program using Python 2.
When students have successfully run their program they will be excited to begin tweaking the code small ways to change what is seen in their world. Make sure students have some time to play with changing variables. If it doesn’t work for one student, have them troubleshoot by comparing code that is working on another Pi.
There are a myriad of ways to edit text files and code built into the Raspbian distribution; several even inside the terminal. But here we will cover some of the easier to use graphical programs.
It’s important you know your way around the terminal before continuing, please see the terminal tutorial -> here
Editing config files with leafpad
Leafpad is a lightweight notepad-like text editor that is bundled with Raspbian; we will use it here to edit simple config files in the file system.
For example the led ring tutorial directs us to edit the /boot/config.txt and comment out some lines.
To do this navigate to the /boot/ directory in the terminal; and then run ‘sudo leafpad config.txt’, this is similar to other commands you have seen taking a path as an argument, the important difference is the ‘sudo’ prefix which tells the terminal to run this command as a ‘super user’. This allows us to overwrite important system files and access gpio pins ect. You’ll have to use it often but you should still be careful.
We get an editor window like notepad on windows, we can insert a ‘#’ before dptparam to comment it out, and then save with control s
after exiting the editor you will regain control of your terminal.
Editing and running python scripts with Thonny
Next we can use a slightly complicated editor, Thonny is specifically for python ‘.py’ files and has some features that make debugging scripts easier.
Thonny can be found in the menu in the top left corner, or again opened from the terminal with ‘sudo thonny’.
Most scripts accessing the GPIO require sudo powers to work correctly, so its best to get used to running it as sudo.
You open a file to edit by navigating to it in the thonny gui; like notepad on windows.
We can enter python scripts line by line into the main window, and at any moment run our script with F5 or the gui button (green arrow).
Here is what it looks like to run a simple program inside thonny.
note the output in the lower window, this is the result of pressing F5 or the run button
The Sense HAT was developed by the folks at Raspberry Pi as an add on board that contains a number of sensors and a LED display. In this lesson we can demonstrate how to use the Sense HAT in conjunction with Minecraft to visualize real world data and control the LED Screen.
I use this lesson primarily to get students excited about the connection between a sensor and the way that we can visualize data through Minecraft. This worksheet from the folks at the Raspberry Pi foundation is an excellent resource. In the latter portions of the lesson it goes into methods of collecting data from the Sense HAT and displaying it within the Minecraft world.
Follow along with the Sense HAT installation and the first several worksheets of this project.
Github is known as a repository for cool programs that can be used in all sorts of ways on your Pi. In this lesson we will learn how to install programs from Github using the terminal.
First lets install a program that can do screen captures. Something that is handy when students have created something amazing in their Minecraft world.
Lets follow the instructions for taking screenshots provided by Martin O’Hanlon.
Here is a video going through the process.
Students will be really excited when they learn to download programs that can run within their Minecraft world. Lets go back to Martin’s API tutorial and have students select one of the first three items, clock, cannon, or snake video game.
In each of these examples, there is a section of code that should be copied into the terminal. When enter is pressed on the keyboard, a bunch of code will appear and a new program file will be placed in the home directory on the Pi
Have students navigate to the file folder and open the file with the .py extension by right clicking on the file and selecting open with python 2 from the drop down menu. If the program is run while Minecraft is open it will place an exciting new feature in the Minecraft world. This will hopefully trigger a desire to try downloading other python programs to modify their Minecraft worlds.
Carefully observe the ports and connect the positive line to the 5v, negative to ground, and the data out line to pin-14 also known as TXD on the raspberry pi GPIO.
Step one is to download the DHT-11 Python library, which is very small and simple to use
Navigate to whatever directory you want in the terminal and use
git clone https://github.com/szazo/DHT11_Python
To download the library.
Then navigate into the directory with cd, and look around with ls
We can already run the example dht11_example.py with
sudo python dht11_example.py
Next we can move on to writing our own program that uses the sensor.
If you need to see an example, here is the full project we will be writing in this article finished.
First we need some boilerplate
import RPI.GPIO as GPIO import dht11 import time #warmup our GPIO pins GPIO.setwarnings(False) GPIO.setMode(GPIO.BCM) GPIO.cleanup() #get ready to read data with pin 14 instance = dht11.DHT11(pin=14)
Next we can start writing our own code. Similar to the clock tutorial we will use a infinite loop to keep our code going
while True: result = instance.read() if result.is_valid(): print("Temperature: %d C" % result.temperature) time.sleep(1)
Our code actually does less than the dht_11 example, but it’s a bit simpler.
result = instance.read()
First we store our input from the sensor in the ‘result’ variable
Next we use an if statement to only proceed if the result is valid
print("Temperature: %d C" % result.temperature)
In our if block we print the temperature using the print function, the ‘%d’ acts as a placeholder for a number (digits), followed by a list of variables to fill the placeholders
print("number %d %d" % (1, 2))
For example would print “number 1 2” to the terminal
Our output should look something like
"Temperature: 20 C"
After exiting the if block we wait for a full second before running the loop again
Remember to exit the program while its running use Control-C to escape
Here is the full example code from this tutorial