Get started on IoT programming with Codebug

For less than $20, Codebug sets the foundation for building simple wearable devices

Get started on IoT programming with Codebug

Programming Internet of things devices isn’t like programming a PC or a mobile device. There’s not much memory, no screen or keyboard, and often no operating system. So how do you get started?

One low-cost option is the Codebug, built around the common PIC 18F microcontroller, an eight-bit SOC. It’s currently available for less than $20. For that you get a USB-connected developer board with four I/O pins, a five-by-five grid of LEDs, a pair of buttons, and a battery option that means you can use it to build simple wearable devices. There’s also an expansion port that lets you connect a Codebug to more complex devices, such as a Raspberry Pi.

The designers intended Codebug to be used in primary education because it’s a cheap way to get code and computers into children’s hands. But it’s also a good, quick introduction to working with the lower-end hardware that will form the basis of the Internet of things. Not every device will have a network connection, and most won’t have even an ARM or Intel processor.

Codebug is an interesting example of a device that used to be the province of specialized controller developers, now available for less than the cost of a pair of movie tickets. Where those specialized developers used C compilers and firmware libraries, things are a lot simpler for the rest of us. With Codebug, development is handled by a cloud-hosted development environment and compiler, with firmware downloaded to your PC and transferred to the Codebug’s 45KB of flash storage over a USB connection.

Programming Codebug is easy. I was able to build a simple counter application in less than five minutes using the Web development environment’s visual programming tools, found under the Create button at codebug.org.uk. These give you two options for writing code: a graphical development tool called Blockly and a C-like programming language. While you can’t yet edit the C-like language, you can see how the Blockly statements translate to traditional coding environments (and how they’re used by the Codebug compiler).

Blockly is a good place to start with basic IoT development. As with a jigsaw, you can connect together loops, standard functions, and variables, as well as working with Codebug’s I/O ports and its LED matrix. Starting with two wait options, either for button state changes or clock ticks, you can construct applications that use basic logic and mathematical functions to work with analogue and digital inputs.

Other commands let you build and deploy sprites on the LED matrix. Manually constructing LED patterns is possible, as each row and column are addressable as decimal-encoded binary. Sprites can be constructed from a basic five-by-five grid, or you can use built-character sprites to scroll individual characters and strings across the LED. Code block directives handle scrolling direction, giving you a framework for building simple interactive applications that use the I/O ports and buttons to control LED states.

Applications are built dragging and dropping program elements onto a development surface. Commands plug into loops, and color-coded blocks let you quickly see where you’re handling I/O and where you’re working with variables. My first application was a simple timer that scrolled numbers up the LED matrix when you turned on the device. Using the built-in timer tick, it used a character sprite to display the contents of a count variable that incremented with each pass through a loop.

My second application was a little more complex, working with the device’s I/O ports to change LED state. While designed to use crocodile clips to quickly connect a Codebug to external components, the I/O port pads are also touch-sensitive pads. By testing for a pin being grounded, I could use a while loop to repeat the test, flipping a line of LEDs from the bottom of the device to the top when I touched a specific pad.

You don’t need to install your code onto a device to test it, as there’s a simple device emulator built into Codebug’s cloud IDE. You can use it to see LED state, and toggle the I/O ports. When ready, code is downloaded, ready for installation. With programs only a few kilobytes of compiled code, there’s no waiting: You can have your application running on a device in a few seconds -- and as it’s firmware in flash, it’s not lost when you shut down Codebug. However, that means you'll need to reset your device to install your next app.

Integration with Raspberry Pi gives you a lot more flexibility. Where on its own Codebug is a relatively dumb device, with no connectivity, hooking its expansion port to a Pi’s GPIO socket opens up the next stage of IoT development: building a gateway.

Values read on the Codebug’s ports can be processed and delivered to a Pi for further processing and delivery to network endpoints. As Raspberry Pi supports a selection of higher-level operating systems and languages, your device data can be read and handled by Linux and Python or Windows and C#, with a network connection to a cloud service. Here you’re writing basic sensor or actuator code on your Codebug and using Raspberry Pi as an IoT gateway, a role for which its more powerful processor, memory, and network connectivity is much better suited.

With a short learning curve and a basic programming environment that’s surprisingly flexible, Codebug is a welcome introduction to simple controller software development. You won’t build a complete IoT framework here, but you are building code that works with sensors and buttons and basic visual and electrical I/O -- much like the simple devices that are going to be the endpoints of our IoT networks. That’s why devices like this are becoming increasingly important, as they’re intended to help beginners of all ages get started with IoT development.

Copyright © 2015 IDG Communications, Inc.

How to choose a low-code development platform