Programming with sensors



We are listening nowadays about the importance of learning a code language from the school, like a new literacy. Coding skills have mainly being integrated in the curriculum of the Primary and Secondary students in a large number of European countries.

The concept of computational thinking refers to the capacity of the programmers of solving problems in a logical way with some instructions in the computer, providing, as a result, a human interactivity normally through the screen of a monitor and with the help of the keyboard or the mouse.

The Raspberry Pi

So the next step would be to create an original program that made possible to interact directly with the ambient around us. For this purpose, we can use a micro-computer called Raspberry Pi and some electronic components, which detect the changes in a diversity of physical variables like temperature, light, humidity or tilt.

This kind of experiences with programming are usually named with the expression Physical Computing (defined by Tom Igoe and Dan O´Sullivan) and they are normally put in practice by using an interface (like the Arduino board) between the computer and the sensors.

Coding teachers sometimes discuss if it is better to learn programming in Secondary schools with a tool like Scratch2, where you move with the help of the computer´s mouse visual blocks to define the instructions on the screen, or by using a compiler with a natural language that you write in a console of programming like Phyton3.

By chance, with the Raspberry Pi you can choose between both options to do Physical Computing because they are integrated in the list of applications included by default in the last version of the Raspbian operative system. Going to the education resources of the main web page, you can find some tutorials with inspiring experiences about how to blink a LED, control a motor or detect changes of lightning with a light-dependent resistor.

The GPIO pins

The way to connect the sensors from the outside world to this micro-computer is possible with the GPIO pins (General Purpose Input-Output) which are along the top edge of the board. The only difficulty to work with them individually is that they are identified with numbers put in disorder, so you would need how it is explained here.

But if your main objective is that your students create simple programs with visual blocks, you will be able to add easily the Pi GPIO Extension for the Scratch2 tool, as you can see in this blog. The problem is that this Extension only can to program digital outputs and inputs, so the possibilities of doing Physical Computing are a bit limited, because the signals in the physical world are analogical. Maybe you would need the support from an external project called s2-pi that suggests how to add new extensions for the GPIO pins.

The Sense HAT

A great component to extend the powerful of the Raspberry Pi is by using the Sense HAT, an add-on board which allows you to detect and measure changes of temperature, humidity, atmospheric pressure and tilt. It also includes an 8×8 matrix of LEDs and a small joystick.

The usual examples are writing with Phyton language and there is an emulator between the applications of the Programming menu which provides more code examples classified in three levels of difficulty (you can use them with the real board by changing in the first line of code  sense_emu  to sense_hat)

I have discovered that in the new version of Scratch2 for Raspbian there is a new Extension for this board which facilitates surprisingly the way of doing basic Physical Computing with the Raspberry Pi. If you open the program, at the Scripts menu there is a part called “More Blocks” where you can add an Extension for the Sense Hat.

When you enlarge this extension on the screen, it appears different possibilities of coding with the matrix of LEDs or with the different sensors. You can light each LED or write letters and messages with some instructions from the new menu, which allow you to change the colour easily. Also, the different sensors (temperature, humidity, pressure, roll, pitch and yaw) are represented in grey as numerical variables to put inside the blocks.

In the simplest program, a Script can move a number of steps (including to draw a line), turn some degrees or say a number which had been detected by the different sensors, for instance by moving slowly the microcomputer with your own hand or by putting it into a small bag and blowing inside.


A great number of possibilities of coding with creativity are opened by using the Raspberry Pi and the Sense Hat to make Physical Computing in a very easy (and cheap) way for students of any Primary and Secondary school.

Article written by: Leopoldo Mosquera, Scientix Ambassador. 

Tags: , , , , , , , , ,

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>