/* jquery */ /* jquery accordion style*/ /* jquery init */

25 October 2016

Astro Pi Data: Daily Pattern Statistics

So far we've seen how to write Python code to that loads and process Astro Pi CSV data files and how to filter the data for a specific date.

In this post we'll using some simple statistics functions to analyse each day in our data set to spot where something out of the ordinary happened.

To begin load up the date filter code and add a new import at the top (note you'll need to use Python 3.4 or later for this library module):.

import statistics

Now add these two new function definitions:

The get_first_date() function finds the first date in the date set. While the second collect_stats() calls this function before looping through all the rows to build date-specific data list for the specified column.

Then we call the statistics mean and variance statistical functions of our date-specific data collection. These values are then printed out.

Finally, in the main code area, we simply call the collect_daily_stats() function with our chosen column number.

As you can see from the output there was a big variance in the humidity data on 21st February 2016.

2016-02-17 humidity: count=4740 mean=45.41 variance=0.12
2016-02-18 humidity: count=8565 mean=45.54 variance=0.68
2016-02-19 humidity: count=8555 mean=45.22 variance=1.97
2016-02-20 humidity: count=8579 mean=44.90 variance=0.12
2016-02-21 humidity: count=8557 mean=49.07 variance=20.28
2016-02-22 humidity: count=8559 mean=47.65 variance=0.47
2016-02-23 humidity: count=8559 mean=47.12 variance=0.73
2016-02-24 humidity: count=8564 mean=47.34 variance=0.31
2016-02-25 humidity: count=8559 mean=46.86 variance=0.12
2016-02-26 humidity: count=8570 mean=46.49 variance=0.23
2016-02-27 humidity: count=8560 mean=45.73 variance=0.14
2016-02-28 humidity: count=8561 mean=45.71 variance=0.08
2016-02-29 humidity: count=8575 mean=45.02 variance=0.08

The Python statistics library model has many other functions, so have some fun experimenting with some other statistical techniques.

Start coding today with my Learn Python on the Raspberry Pi tutorial.

1 September 2016

Astro Pi Data: Python Date Filter

In my previous Astro Pi Data post I showed how to load and process the downloadable Astro Pi Science CSV files using Python.

As I've already mentioned these CSV data files have over 100,000 rows. With this mind let's look at how to extract the data for one particular day.

We're going to start with the Python Basics code from last time, then add a new function and replace the processing loop.

Our function needs to take two parameters: the full list of rows and the date in question. It returned a new list containing only the date-matched rows.

Here's the Python 3 code:

Now we can call this function to populate a list variable with the date-specific data. After this we'll replace the previous row processing loop with one that iterates through the returned list.

Here's the code:

Next time we'll write code to identify interesting days using statistical methods.

Start coding today with my Learn Python on the Raspberry Pi tutorial.

22 August 2016

Astro Pi Data: Python Basics

The adventures of Ed and Izzy aboard the International Space Station (ISS) have generated plenty of interest the the Raspberry Pi community and beyond. And so has the downloadable science data captured by sensors on the Astro Pi modules.

However, these CSV data files are large, even the smallest has over 100,000 rows. Consequently loading a CSV file into a spreadsheet for analysis is a long, slow process. A far more flexible option is to write some code. But how do you get started?

Here's a short Python 3 program (see below) that loads the CSV data in preparation for further analysis. Let's look at the code.

At the top we the Python module library imports followed by a few column number constants. You can add as many of these as you need. Here's the full column list.

Next we have the 'load_csv' function definition. It's this function that extracts data from the file and puts it into a two dimensional list, corresponding to the rows and columns of the spreadsheet. Then it extract the column names, from the first row, before removing this row. It returns the column names and data list.

Now we can call the 'load_csv' function with the CSV file name and store the returned column names and data list in variables.

Finally we set up a row processing loop. Here we just loop through the first 100 rows to get the humidity and timestamp data. But you could choose an alternative range or different column. Next time we'll create a data filter.

Start coding today with my Learn Python on the Raspberry Pi tutorial.

22 July 2016

Astro Pi Data: CSV Columns

The downloadable Astro Pi Science Data CSV files contain quite a few columns. The scope includes Astro Pi temperature, ambient temperature, relative humidity, air pressure, 3D orientation, 3D acceleration, 3D magnetic field strength and 3D rotation.

Thankfully they all have meaningful names. Here's the full list:

row_id: Unique row identity number for each row
temp_cpu: Raspberry Pi CPU temperature degrees in Celsius
temp_h: Ambient temperature in Celsius (Sense HAT humidity sensor)
temp_p: Ambient temperature in Celsius (Sense HAT pressure sensor)
humidity: The percent relative humidity (Sense HAT humidity sensor)
pressure: Air pressure in millibars (Sense HAT pressure sensor)
pitch: Pitch orientation angle from 0 to 360 degrees (Sense HAT accel + gyro + mag data)
roll: Roll orientation angle from 0 to 360 degrees (Sense HAT accel + gyro + mag data)
yaw: Yaw orientation angle from 0 to 360 degrees (Sense HAT accel + gyro + mag data)
mag_x: X axis magnetic field strength in micro-teslas (Sense HAT magnetometer)
mag_y: Y axis magnetic field strength in micro-teslas (Sense HAT magnetometer)
mag_z: Z axis magnetic field strength in micro-teslas (Sense HAT magnetometer)
accel_x: The acceleration intensity of the X axis in Gs (Sense HAT accelerometer)
accel_y: The acceleration intensity of the Y axis in Gs (Sense HAT accelerometer)
accel_zv: The acceleration intensity of the Z axis in Gs (Sense HAT accelerometer)
gyro_x: X axis rotational intensity in radians per second (Sense HAT gyroscope)
gyro_y: Y axis rotational intensity in radians per second (Sense HAT gyroscope)
gyro_z: Z axis rotational intensity in radians per second (Sense HAT gyroscope)
reset: Copy of the Raspberry Pi CPU reset register
time_stamp: Date and time of data readings (Astro Pi real-time clock)

Also see my Astro Pi Data: Python Basics tutorial post.

19 July 2016

Astro Pi Data: Build Your Own Astro Pi

Would you like to build your very own Astro Pi to perform scientific experiments?

Well all you need is a Raspberry Pi board, a camera module and a Sense HAT. Some experiments can be done with the camera alone, but it's the Sense HAT that offers the most experimental potential.

Priced at under £30 the Sense HAT is a true multi-functional beast. Here's the feature list:

• 8×8 RGB LED display
• Gyroscope
• Accelerometer
• Magnetometer
• Temperature sensor
• Humidity sensors
• Barometric pressure sensor
• Mini 5 button joystick

For robustness Ed and Izzy are housed in a custom-made 6063 grade aluminium flight case. Yet you can even replicate this feature too as a Raspberry Pi blog post explains how to fabricate an Astro PI case using a 3D printer.

Also see my Astro Pi Data: Python Basics tutorial post.

29 May 2016

Learn Python - Tom And Tim Stomp

Creating turtle called Tom was fun. But why stick with just one turtle?

Let's write a program that creates a pair of turtles, Tom and Tim, and control them both at the same time. Here's what you'll see...

Let's get coding. Open a new file in your editor and save it as turtle-pair.py, then type in the code below:

from turtle import *

# create Tom
tom = Turtle()

# create Tim
tim = Turtle()

# starting positions

# stomp around
dist = 8
for i in range(30):
  dist += 2


After the import statement we have two code blocks that create Tom and Tim. These turtles have different body and pen colours.

Next we send the turtles to their respective starting positions. Notice that while Tim moves forwards, Tom moves backwards. This gives each turtle their own space to move around in. Once again we'll use the penup command to stop the turtles drawing.

Now we come to the main movement loop.

Inside the loop we've used the stamp command to leave a coloured impression of our turtles on the screen. The 'stamping' loop moves the turtles in a circular motion. The every increasing step distance is specified by the dist variable. We add two to this value every time the loop repeats.

Save the code, run the program and watch Tim and Tom perform some synchronised stomping!

A post from my Learn Python on the Raspberry Pi tutorial.

20 April 2016

Coding with Raspbian Jessie


Right from the beginning Python has been the primary development language of the Raspberry Pi.

Install the latest Raspbian Jessie distribution image and you'll find both Python version 3 and the older version 2.7. Both versions come with plenty of source code examples.


With Scratch, from MIT's innovative Media Lab, you can construct all kinds of multimedia projects without writing a single line of code. Scratch's highly visual interface is based around graphical, snap-together programming blocks and aimed at anyone old enough to use a keyboard and mouse.

On Jessie there's a built-in GPIO server, which simplifies GPIO pins access. Look at little closer and you'll discover this server also delivers direct access to the Pi's built-in camera interface and network IP addressing.

Parents and teachers might like to go to the ScratchEd online community where they can share stories, exchange resources and ask questions.


With the Jessie image Java fans have two new applications in the main menu's Programming category.

Called BlueJ and Greenfoot they simplify the app building experience with a graphical coding environment that mixes mouse-driven design and code generation with traditional hand-coded statements.


Just like any other Linux-based operating system Raspbian's kernel code, device drivers, tools, utilities and graphical desktop are all written using the C language. However, learning a compiled language like C is a little more challenging.

The simplest way to begin is to play around with the pre-installed sample C programs. You'll find graphics, audio and video source code files, associated compilation 'make' files and a couple of helper C libraries in the directory /opt/vc/src/hello_pi.


Node-RED aims to deliver an easy-to-use development tool for Internet-of-Things (IoT) devices, including the Raspberry Pi. Created by IBM it's a browser-based tool built on top of Node.js, a popular free-to-install JavaScript server.

The user interface has a drag-and-drop flow editor, where you wire together combinations of blocks selected from a palette. Once constructed flows can be executed with a single click. Node-RED example flows include GPIO and camera interaction, web services, social media and Pi Minecraft communication.

Sonic Pi

With Sonic Pi you can compose your own unique musical compositions. It was created by Sam Aaron at the University of Cambridge, who hosts live, code-generated musical performances.

The Sonic Pi scripting language is designed around a set of simple, music-centric commands. The syntax will look familiar to those with experience of scripting languages like Python, although it's based on another popular programming language called Ruby.

Code Editors

There are a number of code editors pre-installed on Raspbian Jessie. Some are straightforward Linux text editor stalwarts like Vim and Nano.

Others, like Python's IDLE and the general purpose Geany, understand code syntax and so feature colour-coded listings, auto-completion, debugging support and general coding assistance. Geany supports many other languages including C, Java, HTML, XML and PHP.

Start coding today with my Learn Python on the Raspberry Pi tutorial.

31 March 2016

Windows IoT on the Raspberry Pi 3

At the same time as the Raspberry Pi 3 announcement a Microsoft blog post talked about the Pi 3 compatible preview version of its Windows 10 operating system for IoT Devices.

To discover more about Windows 10 IoT Core development visit the Windows 10 IoT website.

Microsoft also said it is in talks with Element14 about pre-loading Windows 10 IoT Core onto Pi devices.

However, with its 1.2GHz 64-bit quad-core ARM Cortex-A53 CPU and 1GB RAM the Raspberry Pi 3 might be powerful enough to run a full version of Windows. And that would be an interesting development.

Begin Pi 3 coding with my Learn Python on the Raspberry Pi tutorials.