Stephen Smith's Blog

Musings on Machine Learning…

Fortran Lives

with 3 comments


I was looking at a programming IDE called Code::Blocks that seems popular on the Raspberry Pi. Much lighter weight than Eclipse or Visual Studio, but it has a great deal of the same functionality including debugging, project templates, code highlighting and auto-complete. One thing that caught my interest was that Code::Blocks has direct support for Fortran programming. Back when I took first year Computer Science at UVic, all the programming labs were done in WATFIV (Waterloo Fortran IV) and then one of my Co-op work terms was at the the Institute of Ocean Sciences analysing experimental data from the Arctic, writing in Fortran using the extensive Fortran scientific and visualization libraries that were state of the art for the time (on a Univac mainframe).

One thing I noticed when playing with Machine Learning and Python programming was that if I installed something that didn’t have a binary for the particular flavour of Linux I was using then it would download the source code and compile that. What surprised me was the number of Fortran source files it would compile. Part of the reason for this is the giant library of efficient and well tested numerical libraries written in Fortran that have either been donated to the public domain or originated that way from various University research projects. It turns out that many of the popular array, numerical and algorithm libraries for Python are all written in Fortran and a large part of the popularity of Python is the easy access to all these libraries (like LAPACK).


Many people think of GCC just as the Gnu C compiler for Linux. However officially GCC stands for the Gnu Compiler Collection and supports C, C++, Objective-C, Objective-C++, Fortran, Java, Ada, Go and OpenMP. This then provides a great free cross platform compiler for Fortran that supports most of the things in the latest standard. As a result you can do Fortran programming easily anywhere including the Raspberry Pi. GCC is the compiler that the Code::Blocks IDE runs to compile your Fortran programs.

Fortran isn’t pre-installed on the Raspberry Pi, so to compile a Fortran program you need to add Fortran support to GCC which you do from a terminal window by:

sudo apt-get install gfortran


I thought as an experiment I would port my simple flashing light program that I showed in Python and Scratch in my previous blog posting on breadboarding over to Fortran. So Python and Scratch come with GPIO libraries pre-installed and was really easy. For Fortran I didn’t see any such library, but fortunately for Unix/Linux you can usually access any hardware devices via the file metaphor. Basically you control the hardware by opening a file (usually in /dev) and then cause things to happen by writing various things to these special files. Similarly you read these files to get input or status. A good description of how to control the GPIO lines via files is given here. For instance if we want to turn on our LED we wired to pin 17, we would send 17 to the file /sys/class/gpio/export. We can do this from a terminal command line via:

echo 17 > /sys/class/gpio/export

This causes a number of files under /sys/class/gpio/glio17 to be created that we can now access. First we setup pin 17 for output via:

echo out > /sys/class/gpio/gpio17/direction

Then we turn pin 17 on via

echo 1 > /sys/class/gpio/gpio17/value

And turn it off via:

echo 0 > /sys/class/gpio/gpio17/value

Since most programming languages have file I/O libraries, under Linux this then makes most devices accessible.

Fortran Code

Creating the program was quite easy. I found a library on the Web that provides the GPIO access via the files which I’ll list later. The only problem was that the Fortran sleep function takes its parameters only in seconds so you can’t do a sub-second sleep which means the LEDs flash very slowly. Fortunately it turns out that you can call C runtime routines from Fortran fairly easily, so I include the interface to the C usleep (sleep in microseconds) function so we can use the same 0.2 second time between flashes that we used before. Below is the Fortran source code:


! Flash the GPIO lights
program main
  use gpio
  implicit none
     subroutine usleep(useconds) bind(C)
      use iso_c_binding
      implicit none
      integer(c_int32_t), value :: useconds
     end subroutine
  end interface
  integer i, sleepTime

  call gpioSetOut(17)
  call gpioSetOut(27)
  call gpioSetOut(22)

  sleepTime = 200000

  do i = 1, 10
    call gpioOutput(17, 1)
    call usleep(sleepTime)
    call gpioOutput(17, 0)
    call gpioOutput(27, 1)
    call usleep(sleepTime)
    call gpioOutput(27, 0)
    call gpioOutput(22, 1)
    call usleep(sleepTime)
    call gpioOutput(22, 0)
  end do

Fortran has evolved a bit since my University days. There are full structured programming constructs, so you don’t need to use labels. Notice the DO loop has a DO END rather than specifying a label on a CONTINUE statement as we used to do. Fortran has added good support for modules and even object oriented programming while maintaining compatibility for all those scientific libraries everyone loves so much.

Now the GPIO module which I found here. This shows how to create a nice Fortran module. It also shows some Fortran string processing. The module didn’t work for me directly, I had to add a sleep statement after allocating the GPIO pin to allow time for the files to be created or I got file access errors. Here is the code:


module gpio
    implicit none
    subroutine gpioSetOut(pin)
        integer, intent(in) :: pin
        character (len=35) :: unexportChar
        character (len=33) :: exportChar
        character (len=45) :: directionChar
        write (unexportChar, '("echo ", I0, " > /sys/class/gpio/unexport")') pin
        write (exportChar, '("echo ", I0, " > /sys/class/gpio/export")') pin
        call execute_command_line(unexportChar)
        call execute_command_line(exportChar)
        call sleep(1)

        write (directionChar, '("echo out > /sys/class/gpio/gpio", I0, "/direction")') pin
        call execute_command_line(directionChar)
    end subroutine gpioSetOut

    subroutine gpioSetIn(pin)
        integer, intent(in) :: pin
        character (len=35) :: unexportChar
        character (len=33) :: exportChar
        character (len=45) :: directionChar
        write (unexportChar, '("echo ", I0, " > /sys/class/gpio/unexport")') pin
        write (exportChar, '("echo ", I0, " > /sys/class/gpio/export")') pin
        call execute_command_line(unexportChar)
        call execute_command_line(exportChar)
        call sleep(1)

        write (directionChar, '("echo in > /sys/class/gpio/gpio", I0, "/direction")') pin
        call execute_command_line(directionChar)
    end subroutine gpioSetIn

    subroutine gpioOutput(pin, value)
        integer, intent(in) :: pin, value
        character (len=45) :: valueChar

        write (valueChar, '("echo ", I1, " > /sys/class/gpio/gpio", I0, "/value")') value, pin
        call execute_command_line(valueChar)
    end subroutine gpioOutput

    subroutine gpioRead(pin, value)
        integer, intent(in) :: pin
        integer, intent(out) :: value
        character (len=28) :: valueChar

        write (valueChar, '("/sys/class/gpio/gpio", I0, "/value")') pin

        open (unit=1, file=valueChar)
        read (1,*) value
        close (1)
    end subroutine gpioRead

end module gpio

One other thing, I needed to add -lgfortran as a linker option in Code::Blocks, since for some reason a freshly created Fortran application project didn’t include the link for the Fortran runtime.

Fortran Evolution

The latest standard for Fortran came was Fortran 2008 which came out in 2010. The next version will be Fortran 2018 and we’ll see if it shows up next year. GCC supports most things in Fortran 08 plus a few other enhancements. The next 2018 release is includes quite a few things including more advanced parallel processing capabilities and easier interoperability with C. Certainly Fortran has made great strides since my University days with structured, modular and object oriented programming support along with variable length string processing. Fortran really excels at parallel processing support since it is still the goto language on most supercomputers and used for all those advanced climate, astronomical and other scientific models.


I think I’ll stick to Python as my current goto language. But playing with Fortran was fun and it’s amazing to see how much usage it still gets. If you are doing serious high performance numerical processing then you are probably going to have to get your hands dirty with Fortran. Certainly the Raspberry Pi is a good place to learn Fortran with high quality free tools like Code::Blocks and GCC Fortran.



Written by smist08

December 2, 2017 at 4:39 pm


leave a comment »


I’ve been using my Raspberry Pi for retro-gaming, but I was thinking that if my Pi can run old video games for the Apple II and IBM PC then can it also run old business programs? The answer turns out to be yes, with a few notes. The developers of the emulator programs clearly claim their goal is to support gaming, but that of course most other programs will also work. But the emphasis is on user input, general processing and graphics. What is left out or secondary are more business things like printing or networking. The networking aspect would be important if you were trying to run a database server program or a multi-user program that used file locking to control concurrent access.

Anyway it turns out that many older business programs have been provided by the publisher or current own to the Internet Archive for historical preservation. So these are actually available for legal download and playing with. The Internet Archive even links to browser based emulators that can run these in a browser. In this article I’m going to be looking a bit at the history of spreadsheets and the fun fact that you can still run these and have some fun.


VisiCalc is often cited as the first electronic spreadsheet program. It was originally developed and released for the Apple II in 1979 and propelled the Apple II from being a hobbyist computer into a useful business tool. VisiCalc was written in 6502 assembler and was very fast and compact. VisiCalc was ported to the IBM PC when it came out in 1981, written in 8088 assembler, it was only 27K in size. However VisiCalc was a straight port of the Apple version (including bugs) and never utilized the greater hardware capabilities of the newer PC. After Lotus supplanted VisiCalc they purchased what was left in 1985 and eventually donated VisiCalc to the Internet Archives for historical preservation. So let’s give it a try on the Raspberry Pi, running under the various Retro Pie emulation libraries. First with the Apple II. I couldn’t get it to work in 80 column mode or with lower case letters, but otherwise seemed to be working fine. I couldn’t figure out the emulator’s screen capture function so I just took a photo of my monitor.

Using VisiCalc reminded me how bad the original Apple keyboard was. It didn’t have many keys so VisiCalc used escape for backspace, and there were only horizontal arrow keys so you had to toggle between moving vertically or horizontally using the space bar.

Now let’s try it in a DOSBox to emulate an original IBM PC. Here upper/lower case work, its 80 columns and there are all 4 direction keys, so moving around is much more natural.

The PC version of VisiCalc isn’t that bad. It’s just too bad the developers spent all their time porting to different platforms and didn’t develop the feature set or recognize where the spreadsheet world was heading.

Dan Bricklin, one of the founders of VisiCalc is still around and a frequent writer and blogger. He has quite a bit to say on technology evolution and the history of VisiCalc and Lotus. You can check out his web site here. One thing he points out is that because VisiCalc was so popular, that anyone entering the market had to be compatible so Lotus 123 had to read VisiCalc files seamlessly and it ended up copying VisiCalc’s expression language and structure pretty closely. As a result no VisiCalc customers were orphaned or abandoned since they could easily and seamlessly move to Lotus 123 and then Excel and now Google Sheets. If you do play with VisiCalc, it’s worth having a look at the reference cards that Dan posted here.

Lotus 123

With the advent of the original IBM PC with better hardware (mostly more memory for programs to run, but with the original 640k limit), the stage was set for someone to supplant VisiCalc who just did a straight port. Enter Lotus 123 in 1983 which claimed to be a spreadsheet, graphics and database program (hence the 123). The database claim was rather dubious but the graphics claim of producing business charts and graphs from your spreadsheet data was groundbreaking for the time. The first couple of versions of 123 were written in 8088 assembler and went around the BIOS and DOS to access the hardware directly. This made the program blazingly fast. Eventually 123 was supplanted by Excel and Lotus was bought by IBM in 1995 for a whopping 3.5Billion. IBM donated some early versions of 123 to the Internet Archives. I tried downloading and running 1.0a. This was a good version to play with since I didn’t have to install anything, I could just run it directly from the original floppy disk image. Below is a photo of my screen running Lotus 123 1.0a on my Raspberry Pi under the RetroPie MS-DOS emulator.

Trying to view graphs didn’t work in the emulator, I suspect this oldest version of 123 expects a graphics mode that the emulator hasn’t spent much time on (ie pre-VGA and probably pre-EGA).

Lotus 123 lost the battle when the world went to GUIs and Microsoft’s Excel became the new defacto spreadsheet program. IBM shutdown 123 development in 2014 and put this program to bed for good. Of course migration from 123 to Excel is painless so no one was left in a lurch due to this.

What’s Next?

A common question is: As 123 supplanted VisiCalc and as Excel supplanted 123, what’s next? I think one thing to be aware of is that Lotus 123 was dominant for a long time and its decline was slow. I think the seeds of replacing Excel are already sown. Now Microsoft isn’t just a one product company so it can keep Excel (and Office) going for a long time, but the contribution of Office to the bottom line is going down and Microsoft is looking to other areas for proper growth (like Azure). It might not be that there is just one replacement, since spreadsheets are used for many things, at different groups of users may take different paths. Some of the alternatives are:

  • Cloud based like Google Sheets. These offer much better collaboration and your data is kept safe in the cloud. You can run these on all your devices from your phone to your workstation. Many would argue that Microsoft is doing well here with Office 365, but I think Office Online is an also ran to Google Sheets just as Lotus 123 for Windows was an also ran to Excel.
  • Free software. LibreOffice is now really good and does most things people need. Why would anyone pay for Office when LibreOffice is there. Note that LibreOffice is also pre-installed on the Raspberry Pi.
  • Per device specialist. For instance Apple has Numbers which is freely available for all their devices as an app or for other platforms as a web app. This is very tailored to Apple devices and many will use this since it is usually pre-installed.
  • Specialty apps. People don’t seem to be going in for big giant programs anymore. The in thing are small task specific apps that typically run on a mobile device. For many tasks rather than use a spreadsheet, people just install a specialty app from their app store to get the job done.
  • Accounting apps. Many accounting companies like Xero, Sage or Intuit have extremely easy to use entry level products. Much of the use of spreadsheets is to keep simple general ledgers. People perceived using proper accounting apps as too difficult or too expensive, but this is no longer the case. Similarly most banks have really good web interfaces that give you most of what you need.
  • Machine learning and advanced analytics. This field has also moved away from spreadsheets and into more sophisticated tools and often more pure code  solutions. Much advanced analytics is now performed in R or Python since the quantity of data has overflowed spreadsheet usage.
  • Digital assistants, perhaps you don’t need to calculate with a spreadsheet anymore. Now you can just ask your digital assistant a question and get an answer. Digital assistants can have access to your accounting data and other data to give an answer without you needing to know formulas or type in any data. You can even type questions or formulas into Google directly and often just get a good answer. Its really good at metric conversions and other common tasks.


Playing with retro-business apps is almost as much fun as playing with retro-games. It’s certainly fun to be able to run all these older programs under emulation on my Raspberry Pi. It is also amazing how performant they seem and what a good job the open source communities that work on these emulators have done.

There are many other spreadsheet programs that you can play with, but I chose these two since I actually used them and they were industry leaders in their time. Some of the other abandoned spreadsheets include SuperCalc, Multiplan, T/Maker, Symphony, Jazz, Improv and Quattro Pro. But there is only so much time and this is already quite a long article.

Written by smist08

December 1, 2017 at 12:13 am

Playing Music by Writing Code

leave a comment »


The standard Raspbian Linux for the Raspberry Pi comes with an interesting program called Sonic Pi. This program lets you create music by writing code, the goal is that the coding is easy enough that anyone can learn it, and that you can code fast enough to play a live performance by writing the code as you go along, including jamming with other musicians. From my coding experience there is always a lot of pressure to write code faster, but here you need to write the next bit of code and stay on beat.

I was interested in this, primarily to use as a drum machine to accompany by very beginner guitar playing. Basically something a bit more interesting than a metronome. Sonic Pi will run on Windows, MacOS and Linux, but one of the goals of the program was to be as accessible as possible, so hopefully even low income school districts or third world countries can afford a Raspberry Pi or two.

Learning to Code

Like many programs on the Raspberry Pi, Sonic Pi is a backdoor way to try and get kids to learn how to code. Writing code to create music has been around since electronic synthesizers became digital, but how to do this was quite technical and required a lot of (then) expensive equipment. Sonic Pi comes with a lot sampled sounds so you can go quite far without a good sampler.

The programming language is based on Ruby and can do some quite powerful things. The emphasis in Sonic Pi tends to be on fairly straight forward functional programming and it recommended that you only use what is documented for Sonic Pi, but people have discovered hidden Ruby features that work if you try them.

The Sonic Pi language is described as a strongly timed language (a play on other strong typed languages) since music is all about timing and Sonic Pi is brilliant in how it keeps everything in time.

The language keeps things simply, so hopefully if a student can think musically then they can transfer that skill into programming via the Sonic Pi language. The hope being that this is a more interesting entry point to programming than say learning sorting algorithms.

The program is designed to be interactive, have lots of help and typical IDE type features like auto-complete. There is a vibrant online community and tons of examples. Additionally there is a free book available here.

Live Performances

People actually use their Raspberry Pi running Sonic Pi as a musical instrument. This isn’t just running a program they wrote previously for people to listen to. This is actually writing the code as people listen. It isn’t always a solo performance either, it could include jamming with other musicians either playing Sonic Pi or playing regular musical instruments.

The key part of the language to support this is the live loop, where you have a loop playing the music (or code) and it allows you to update the code and have the new code seamlessly take effect whenever you like without causing a break in the music.

This usage is often called live coding and there are actual clubs that have events where this happens called algoraves.

It’s hard to explain this, but if you check out some of the YouTube videos of such performances it’s easy to get the idea. A good introduction is Sam Aaron’s presentation at OSCON on how to start coding and playing Sonic Pi. Here is a Sonic Pi performance covering Daft Punk. Here is a jam session with a guitarist. And another example.

Some Code

At the most basic level you can get some noise out of Sonic Pi just by running the one line program:

play 100

This just plays a beep which decays to nothing. You can add another note with:

play 100
play 75

This actually plays the two notes at the same time, since the program doesn’t stop and wait for the first note to finish. To play the two notes one after the other we would do something like:

play 100
sleep 1
play 75

The notes are actual midi numbers from 0 to 254. The following diagram shows how they relate to the notes from a piano keyboard.

Beeps are all well and good, but you can get much more interesting notes by playing the built in samples. For instance:

sample :bd_haus
sleep 1
sample :ambi_choir

We aren’t very musical yet, but if we put it in a live_loop then it will play over and over.

live_loop :mySong do
    sample :bd_haus
    sample :ambi_choir
    sleep 1

Now we’re playing music and with the live_loop we can modify the code and when we hit run it seamlessly starts playing the new code allowing us to perform live by writing code.

Rather than including a lot of source code in this article, for more examples I’ll just provide a couple of links. Note that if you Google around, there are a lot of Sonic Pi samples that you can have a look at. A good little drum machine bit of code is this one. Since Sonic Pi is based on Ruby you can use Ruby string processing to convert ascii drum tabs into music as done here.


Sonic Pi is a pretty cool program. Basically you get a fairly sophisticated music synthesizer with your inexpensive Raspberry Pi. It’s fun to play with, and perhaps will motivate a few more kids to pick up coding. After all if you do learn how to program in Ruby with this, then you are well on your way to being a highly employable Ruby on Rails web developer. Combined with Scratch, the Raspberry Pi really offers some innovative ways to teach coding to students.

Written by smist08

November 25, 2017 at 9:25 pm

Retro-Gaming on the Raspberry Pi

with one comment


Retro-gaming is really popular right now as exemplified by how hot the Nintendo SNES classic gaming systems are which outsold both the PS4 and XBox One last month. However you can put together a similar gaming system with a Raspberry Pi, some game controllers and a bit of software. This isn’t as easy as just buying an SNES Classic, but you get access to way more games, and games on way more platforms. In this article we’ll start to look at the retro-gaming world on a Raspberry Pi system.


RetroPie is a bit of glue that combines many open source gaming projects into one system that is easy to install and get running. It includes dozens of emulators for many older gaming system from Nintendo, Sega, Atari, Colecovision and Sony. It also includes dozens of emulators for older computer systems including Apple II, Atari ST, Commodore 64 and Amiga, Radio Shack TRS-80, MS-DOS, Apple Macintosh, Amstrad, ZX Spectrum and the TI-99. Its really amazing how many of these old systems have active communities creating and maintaining these emulators. Many are configurations of some overarching all in one emulators, but nevertheless it’s amazing what you can run.

You can configure the system to give you a menu of games to run, or to just autorun one particular game on startup. The system also prefers getting all input from the game controller (which could be a keyboard). Combined these allow people to build their own arcade game boxes.

There is quite a community of DIYers building these themselves from scratch, along with all sorts of kits being available. The other nice thing is that nearly everything in the Raspberry Pi marketplaces is quite affordable.


Although you can use a keyboard to do most things it isn’t ideal. You will probably want a proper game controller fairly quickly. Fortunately these are readily available and quite cheap. Note that out of all the systems that are emulated, most had different controllers, so you probably want a controller close to your favorite system, or you might like to buy a collection of  controllers.

Another reason to use a game controller is that if you use the keyboard, then some game controllers require mapping a lot of keys to buttons and this can then interfere with keyboard usage if it is used as well (more for computer emulations like an Apple II or Commodore Amiga).


So what games can you play? The easiest games to play with Retropie are the games that can be installed from the Retropie package manager. These are all open source games that have been ported to the Raspberry Pi (ie they are natively compiled for the ARM processor). You might think yuck, open source games aren’t good. But you would be wrong, because several software publishers have open sourced their old games as an act of goodwill to their fans. These include games by ID Software including Doom, Quake and Castle Wolfenstein. I found all these games work exceedingly well and I enjoy playing them again.

Next if you install the SNES emulator from the package manager, it is now installed but has no games and there aren’t any games for this in the package manager. This then leads us into the murky world of abandonware. Basically there are websites that contain games from companies that no longer exist. After all how many Atari ST or Commodore Amiga game publishers are still in business? These sites have downloads for many games that you can copy to the Raspberry Pi and run. I say murky because copyright law in different countries have different views on the legality of copyrighted materials, when the copyright holder is no longer around. Anyway you can make your own judgement. If you do have an old SNES system for instance and play games you own then many people consider this ok too. Anyway this topic is up to you. But pretty well every old game you can think of is available this way.


With the computer emulations, you can just run games like the console games, but these tend to be a bit more problematic, since you need to deal with multiple floppy disks (usually hard disks don’t work for games), and many games have copy protections or non-standard disk drivers that can mess up the emulation, so results will vary. I found you can get quite a bit working but it tends to take a bit of fiddling with settings to make things work.

The other cool thing you can do with the computer emulators is just run the old computer software. For instance I used to have an Apple II+ computer that I bought with the money from my first University coop work term. It’s fun to just run Applesoft in the emulator and play with good old Applesoft Basic again. One interesting thing is that by default the emulator is configured to run at ¼ speed. This is so the games play about the same as on an original Apple IIe. This shows how good the emulator is that is can emulate an Apple running 4 times the speed of the original on a little $35 piece of kit.


Retro-gaming is a lot of fun and you can certainly waste a lot of time. They don’t have the high resolution realistic graphics that you can get out of a modern computer with a graphics coprocessor, but a lot of these games were groundbreaking and a lot of the actual gaming elements haven’t changed. If you have an inkling to play old arcade games like Space Invaders, Dig Dug or Ms. PacMan then this is a great way to play around. A lot of these emulators will run on regular PCs or Macs, but there is something fun about doing it on a little 35 dollar Raspberry Pi.


Written by smist08

November 22, 2017 at 4:23 pm

Breadboarding with my Raspberry Pi

with 4 comments


One of the cool things about the Raspberry Pi is that it has a set of general purpose input/output (GPIO) pins. Many Raspberry Pi starter kits (including my Canakit) come with a breadboard and few simple electronic components you can play with. In this article I’ll talk about connecting up some LED lights and controlling them from both Python and Scratch. Below is a bit of a hazy picture of my Raspberry Pi hooked up to the breadboard and a Scratch program running.

Here is a closer look at the breadboard with a few LEDs and resistors connected.

You don’t need to do this for many standard tasks, after all the Pi has four USB ports, Wifi, Bluetooth, HDMI, sound/composite video and ethernet ports. But the GPIO port is great for electronic enthusiasts, hobbyists and educators to get their hands dirty playing with electronic components.

Hooking up an LED

Each GPIO pin can be individually controlled and will provide 3.3V when activated. It is then specified to keep the current under 16mA or you can damage the circuits. My kit came with a number of 220 Ohm resistors and by Ohm’s law these would case the current to be 3.3V/220Ω = 15mA, so just right. You need to have a resistor in series with the LED since the LED’s resistance is quite low (typically around 13 Ohms and variable). I connected 3 LEDs and for each LED you connect a wire from a GPIO pin (in this case I used 17, 27 and 22) to the positive lead of the LED then you connect the negative side to a resistor and the other side of the resistor to the -3.3V line on the breadboard. Really quite simple.


It’s quite simple to control the GPIO pins via a Python package. You just need to import RPi.GPIO and you can get going. This package came pre-installed so all I had to do was write some lines of code and away it went. Basically I just need to set the mode for since the package supports a few different boards and chipsets, then configure the pins I’m using for output. Then I just need to turn the LEDs on and off. You need to add some sleep statements or the whole thing executes faster than you can see.


Scratch is a very simple and visual programming language/environment developed by the MIT Education Department. It is used to teach programming to students as young as in kindergarten. It is really amazing the animations and games that kids can produce with this system. It comes pre-installed on the Raspberry Pi and you can also control the GPIO pins with it, just like you can in Python. You have to run the GPIO server from the edit menu and then you use the broadcast statement to control the GPIO functions. Here is the Scratch version of the simple Python program displayed above.

More on the GPIO

The GPIO has 26 pins, two are +3.3V, two are +5V, 5 are ground and then that leaves 17 as general GPIO pins.

In the same way we configured the pins for output to control LEDs you can configure them for input and then for instance read the setting of a switch.

However this isn’t all there is to GPIO, besides the functions we’ve talked about so far, which are rather limited, a number of the pins have “alternate” functions that you can select programatically. For instance pins 3 and 5 can support the I2C standard that allows two microchips to talk to eachother. There are pins that can support two serial ports which are handy for connecting to radios or printers. There are pins that can support PWM and PPM which are handy for controlling electical motors.


The Raspberry Pi 3 is a very versatile device. It runs a most Linux software and has a very flexible architecture allowing it to interface to a great many devices. It has four USB ports, Wifi, Internet and Bluetooth. Plus there is the general purpose GPIO bus that allows a great deal of flexibility to interface the Pi to almost anything. That is why you see Raspberry Pi’s built as the brains of drones, robots, home security systems, information kiosks and so much more.


Written by smist08

November 15, 2017 at 9:14 pm

Posted in raspberry pi

Tagged with , ,

Playing with my Raspberry Pi

with one comment


I do most of my work (like writing this blog posting) on my MacBook Air laptop. I used to have a good desktop computer for running various longer running processes or playing games. Last year the desktop packed it in (it was getting old anyway), so since then I’ve just been using my laptop. I wondered if I should get another desktop and run Ubuntu on it, since that is good for machine learning, but I wondered if it was worth price. Meanwhile I was intrigued with everything I see people doing with Raspberry Pi’s. So I figured why not just get a Raspberry Pi and see if I can do the same things with it as I did with my desktop. Plus I thought it would be fun to learn about the Pi and that it would be a good toy to play with.


Since I’m new to the Raspberry Pi, I figured the best way to get started was to order one of the starter kits. This way I’d be able to get up and running quicker and get everything I needed in one shot. I had a credit with Amazon, so I ordered one of the Canakits from there. It included the Raspberry Pi 3, a microSD card with Raspbian Linux, a case, a power supply, an electronics breadboard, some leds and resistors, heat sinks and an HDMI cable. Then I needed to supply a monitor, a USB keyboard and a USB mouse (which I had lying around).

Setting up was quite easy, though the quick setup instructions were missing a few steps like what to do with the heatsinks (which was obvious) or how to connect the breadboard. Setup was really just install the Raspberry Pi motherboard in the case, add the heat sinks, insert the microSD card and then connect the various cables.

As soon as I powered it on, it displayed an operating system selection and installation menu (with only one choice), so clicked install and 10 minutes later I was logged in and running Raspbian.

The quick setup guide then recommends you set your locale and change the default password, but they don’t tell you the existing password, which a quick Google reveals as “Raspberry”. Then I connected to our Wifi network and I was up and running. I could browse the Internet using Chromium, I could run Mathematica (a free Raspberry version comes pre-installed), run a Linux terminal session. All rather painless and fairly straight forward.

I was quite impressed how quickly it went and how powerful a computer I had up and running costing less than $100 (for everything) and how easy the installation and setup process was.


I was extremely pleased with how much software the Raspberry Pi came with pre-installed. This was all on the provided 32Gig card, which with a few extra things installed, I still have 28Gig free. Amazingly compact. Some of the pre-installed software includes:

  • Mathematica. Great for Math students and to promote Mathematica. Runs from the Wolfram Language which is interesting in itself.
  • Python 2 and 3 (more on the pain of having Python 2 later).
  • LibreOffice. A full MS Office like suite of programs.
  • Lots of accessories like file manager, calculator, image viewer, etc.
  • Chromium web browser.
  • Two Java IDEs.
  • Sonic Pi music synthesizer.
  • Terminal command prompt.
  • Minecraft and some Python games.
  • Scratch programming environment.

Plus there is an add/remove software program where you can easily add many more open source Pi programs. You can also use the Linux apt-get command to get many other pre-compiled packages.

Generally I would say this is a very complete set of software for any student, hobbyist or even office worker.


I use Python as my main goto programming language these days and generally I use a number of scientific and machine learning libraries. So I tried installing these. Usually I just use pip3 and away things go (at least on my Mac). However doing this caused pip3 to download the C++/Fortran source code and to try to compile it, which failed. I then Googled around on how to best install these packages.

Unfortunately most of the Google results were how to do this for Python 2, which I didn’t want. It will be so nice when Python 2 finally is discontinued and stops confusing everything. I wanted these for Python 3. Before you start you should update apt-get’s list of available software and upgrade all the packages on your machine. You can do this with:

sudo apt-get update        # Fetches the list of available updates
sudo apt-get upgrade       # Strictly upgrades the current packages

What I found is I could get most of what I wanted using apt-get. I got most of what I wanted with:

sudo apt-get install python3-numpy
sudo apt-get install python3-scipy
sudo apt-get install python3-matplotlib
sudo apt-get install python3-pandas

However I couldn’t find and apt-get module for SciKit Learn the machine learning library. So I tried pip3 and it did work even though it downloaded the source code and compiled it.

pip3 install sklearn –upgrade

Now I had all the scientific programming power of the standard Python libraries. Note that since the Raspberry Pi only has 1Gig RAM and the SD Card only has twenty something Gig free, you can’t really run large machine learning tasks. However if they do fit within the Pi then it is a very inexpensive way to do these computations. What a lot of people do is build clusters of Raspberry Pi’s that work together. I’ve seen articles on how University labs have built supercomputers out of hundreds or Pi’s all put together in a cluster. Further they run quite sophisticated software like Hadoop, Docker and Kubernetes to orchestrate the whole thing.


I now have the Raspberry Pi up and running and I’m enjoying playing with Mathematica and Sonic Pi. I’m doing a bit of Python programming and browsing the Internet. Quite an amazing little device. I’m also impressed with how much it can do for such a low cost. As other vendors like Apple, Microsoft, HP and Dell try to push people into more and more expensive desktops and laptops, it will be interesting to see how many people revolt and switch to the far more inexpensive DIY type solutions. Note that there are vendors that make things like Raspberry Pi complete desktop computers at quite a low cost as well.

Written by smist08

November 11, 2017 at 9:35 pm

Getting My Amateur Radio License

leave a comment »


I recently passed the exam to get my amateur (ham) radio license. This article is a bit on my experience with taking the course and writing the exam, as well some thoughts on ham radio. People spend a lot of money on cell phones, but most of what they do is done routinely for free by dedicated radio hobbyists. After all a cell phone is just a radio running on frequencies bought up by the cell phone providers.


There is a perception that amateur radio is an old obsolete dying hobby. That is consists of people using old tube radios trying talk to people far away with lots of interference and poor sound quality.

Why would anyone use ham radio when you can talk to anyone reliably on Skype or FaceTime? Didn’t ham radio only make sense back in the days when long distance calls were prohibitively expensive? Now even most phone plans are quite inexpensive and use the Internet to communicate. So why are the number of amateur radio licenses growing year over year? In the US the number of licenses issued each year is growing at a rate of about 1%, not Internet growth, but pretty steady for something that requires preparing for and writing a written exam.

Here are some of the main reasons for the continuing interest in amateur radio:

  • Disaster preparedness. Most amateur radios can operate off batteries and don’t require cell phone towers, Internet connections or even A/C power.
  • Better radios. There are a proliferation of new radios on the market, at much lower price points (under $100)  and a great many features including computer connectivity, digital channels and more efficient power.
  • Volunteer infrastructure. To use your cell phone you typically pay $75/month to someone for your ability to use their cell phone towers and such. In the amateur radio world many clubs operate repeaters (think amateur cell phone towers) and Internet connections allowing long distance communications with voice or even e-mail from low powered portable radios.
  • Cell phone coverage often isn’t as good as advertised. I do a lot of hiking and mountain biking. Most of the places I go, there is no cell phone coverage. However here on the Sunshine Coast if I get line of sight to the ocean I can probably hit one of the Nanaimo repeaters and communicate. The only other alternative is satellite phones and these make cell phones seem inexpensive.
  • Ability to build your own equipment and experiment. The world of cellphones is very locked down and DIY is not allowed at all. In the world of amateur radio if you have the correct license, you can build your own radio, you can heavily customize your equipment, you can build your own antennas and generally do a lot of experimentation and customization. If you have an interest in understanding how things work and playing with electronics, then this is a great playground.


To get my license I took a course offered by VECTOR in Vancouver to prepare and write the basic amateur radio exam. This was a great success and I greatly enjoyed it. VECTOR is an emergency radio non-profit society for the city of Vancouver. The course was taught by a number of VECTOR members usually someone different for each section. This gave a good mix of perspective and provided a good variety over the five morning sessions. The details of this process are a bit different by country so the details here apply specifically to Canada. The basic exam is 100 multiple choice questions, you can take practice exams here. The course covers the main areas of the exam which are: Rules and Policies, Basic Electronics, Antenna Systems, Radio Wave Propagation, Interference and Suppression, Operating Procedures and Station Assembly Practices and Safety. The whole course and exam are theoretical and don’t involve touching a radio whatsoever.

I did pass the exam. If you get 70% then you get access to all frequencies above 30MHz and if you get 80% then you don’t have any frequency limitations. I got over 80% so I can use any amateur frequency band. My call sign is VA6SMI. I have to use this to identify myself in any communications.

To actually build your own radio you need the advanced certification which I might try to get next year. The other qualification you can get is Morse Code. Up till ten years ago this was part of getting a basic license but isn’t required anymore.


I think a lot of the current popularity of ham radio is driven by a wish to be independent of cell phone providers and a wish for much more DIY in our communications. As recent fires, hurricanes and earthquakes have demonstrated the cell phone infrastructure is rather fragile and can take quite some time to get up and running again. During all these emergencies, ham radio was the main form of communications. With global warming, fires and hurricanes will become more common and a need for emergency communications becomes more and more important.

Separately amateurs are adding more and more functionality into ham communications with better digital protocols and speed. They are adding better volunteer infrastructure to support longer distance communications and Internet connectivity. The radio hardware market has gotten much more competitive recently with the addition of several Chinese manufacturers that have driven down prices and driven new innovation.

Will we reach a point where we have “smart” ham radios like we have “smart” phones? Will we be able to run apps like Facebook on our ham radio? Will be be able to use them as ubiquitously as we use our phones? Will we one day be able to break the hold of the cell phone providers and eliminate all those expensive subscription plans? It might take some time but we are slowly moving in that direction. Certainly the ham radio frequencies can’t handle that volume of traffic, but perhaps for those willing to pass the basic radio exam we will start to get some freedom from the current cell phone hegemony.


I enjoyed the process of getting my ham radio license. I’ve ordered a radio and look forward to getting on the air when it arrives. It was fun reviewing basic electronics and learning more about antennas and radio wave propagation. The rules and regulations part wasn’t bad, once you get the hang of what they are looking for, the questions are quite easy.

Now I’ll see if I’m better connected once I’m out in the woods. Plus I’ll be able to communicate during power outages (we had one today that also took out local cell phone coverage).


Written by smist08

October 21, 2017 at 12:47 am