Google+ Facebook Twitter Pin It Reddit

Jameco Builds logo

An Introduction to BlocklyProp Programming

Coding tools are increasingly high-level and easier to use

By Ken Gracey, Parallax

If you've been to a classroom lately you understand the need to compete with smartphones which encourage a near-instant reward cycle! Here's a quick introduction to Blockly using the ActivityBot 360° and a few sensors from their shop. I've also shared a low-cost way of getting started for around $40.

I've been particularly close to physical computing in education since my employer (Parallax) has been training teachers and students for 20 years. Programming tools have become more high-level, with libraries and functions making it quicker to have a working project.

This is particularly important in short class periods where students need to build, program and see results. This abstraction isn't entirely without drawbacks, though. Controlling a servo motor is often a set-it-and-forget it background task without an understanding of how pulse length relates to servo position. At some point students may not learn the low-level coding required for synchronous/asynchronous serial and I2C communication unless they pursue a degree in electrical engineering.
Activity Bot 360 DegreeActivity Bot 360°

Yet the benefits of these changes are big for me. I've never been much of a coder even though I work for Parallax. I can create a project and write the code to run it, but eventually I have trouble with advanced math, moving from datasheet timing diagrams into commands, and making efficient programs that package up repetitive operations into their own little place for reuse.

Recently, things changed for me and learning has been much faster, easier and way more fun!

Programming microcontrollers a cartoon-looking language?

Blockly, Snap! and Scratch are all "visual programming languages." These languages are popular in elementary school for screen-based games, animations and robots. Visual programming languages have graphical elements for functions, operators, variables, arrays and programming constructs.

Blockly is Google's open-source visual programming language. Blockly is the most useful for programming microcontrollers because the tools are wide open to developers. Blockly blocks can generate any code you put into the JavaScript (C, Python, BASIC, etc). Blockly could be described as a superset of languages in that you can write your program visually in pseudocode. If you can describe what you want to achieve then you can "lay out" a program and see the relationships between functions clearly.

There's no reason not to take these systems more seriously - they're as accurate as the code they generate and this system is no longer just for little kids.

BlocklyProp for the Propeller Multicore Microcontroller

Parallax ported and extended Blockly into BlocklyProp to work with the very-capable Propeller Multicore Microcontroller. Take a look with this piece of BlocklyProp code which plays notes on a piezo speaker connected to P4:

Blockly Prop CodeBlocklyProp Code

The blocks generate C, shown in the editor by clicking on the "code" button.

Blockly for microcontrollers isn't new. Ardublocks is popular with Arduino users and micro:bit are among the many popular systems available. However, Parallax has the most capable Blockly system available at this time. We've needed this system to reduce the barriers to using our own multicore microcontroller.

For example, there's a particularly useful block in BlocklyProp called "new processor" which launches functions to run in their own processor (of which there are eight) with variables shared throughout the program. This little example shows the value of a counter maintained in its own processor, exclusive of whatever time-blocking commands may exist or be added to the main repeat-forever-do loop which simply waits for the user to press a key:

New Processor CodeNew Processor Code

No Interrupts and True Parallel Processing?

I know what you're thinking and it's all true. This means you can generate sound at the same time as you are controlling lights, that you can receive a GPS signal concurrently with controlling motors and that you can get a pushbutton state anytime. It all happens, in parallel.

The capabilities of this system are impressive. If you want to get an idea of the possibilities, start with the BlocklyProp Reference guide linked at the bottom. Browse through the command reference.

BlocklyProp Runs in the Cloud

BlocklyProp runs on our Amazon Web Services servers in the cloud. When you compile or download, your Windows/Mac/Chromebook sends the code to our servers where it is compiled and returned for local download.

The cloud connectivity is sometimes considered a disadvantage where there is no internet: rural communities, trade shows and conferences. But the benefits of a cloud-based system are big, too. These include programming from a browser window, having the latest version of the software always present; and sharing programs with others easily. Students are raised in the Google Drive ecosystem and have fewer needs for local software installations. They also expect to be able to collaborate and share documents (and code!) without "zipping" things up.

Line Following Robot with Color-Coded Turns

The fastest way to learn programming is with a project, so I chose an ActivityBot 360° robot. The idea was to follow an electrical tape line but using color samples on the intersections to denote turn instructions. Teachers ask us for additional classroom challenges, so this is the basis of one of those projects.

ActivityBot 360 degreeActivity 360° Intersection Project

Jameco stocks all of the parts for this project. The project consists of an ActivityBot 360° robot equipped with a QTI Line Follower Kit and ColorPal sensor. The physical arrangement of these parts on the ActivityBot looks like this:

Line Follerer and ColorPalLine Follower and ColorPal

This project is a simplified educational model of an autonomous vehicle. Autonomous vehicles need to interact with their environment for the highest level of safety, using camera systems, artificial intelligence and laser sensors. Admittedly, the complexity of this project is far from the reality of autonomous vehicles, but it gives students a starting point to think about how these systems might actually work.

The pseudocode I had in mind for the project would work like this:
  1. Follow the electrical tape line, adjusting wheel speed to keep it centered. Do this in a tightly-timed loop, 20-50 times a second.
  2. Check for colors with the ColorPal Sensor, and if encountered:
  3. a. Light up a WS2812 RGB with the color detected for visual feedback.
    b. Make sounds concurrent with one of the next three movements:
    • Red is left turn 90 degrees
    • Yellow is right turn 90 degrees
    • Green turns back 180 degrees
  4. Repeat the process
Note that the "new processor" block makes it possible to generate the sound effect while the robot moves off the colored indicator. This is one very simple use of this powerful block.

Here's the project in action:

I've left lots of room for improvement with this project, mostly for the sake of keeping the code base easy to understand. I've used dead-reckoning for the turns and made the intersections T-shaped as an example. The interaction of the code and sensor placement on the robot has much to do with the line following layout, too.

The main loop of the Blockly code:

You can view and copy the entire BlocklyProp code right here.

Blockly is for Baby Programmers!

Not really.

Recently, Dropcopter brought us a problem and we used BlocklyProp as the solution. Dropcopter is competing for $1M in the New York Genius competition to create an effective drone for agricultural pollination. Their pollination system included two servo motors, a stepper motor, and an input from their RC receiver. They had begun the project with an Arduino system which proved difficult to program and couldn't do any parallel processing.


So, with our help, we switched it over to the Propeller Multicore with BlocklyProp, using a Propeller Flip microcontroller module. In two days and with no other new electronics, we had their machine running beautifully, and they now know how to program it without having had any prior programming experience. Their team has a solid understanding of the code environment, and field applications will start within two weeks.

Propeller MulticorePropeller Multicore

Parallax's next project is to use BlocklyProp to program a robotic boat to autonomously navigate the waters of Lake Tahoe, California's oligotrophic "crown jewel of the Sierras". The robot will periodically collect data on surface water turbidity, temperature and pH. BlocklyProp has native blocks for sensors (GPS, lasers), communication (XBee) and motor control. If there's not a block for my need, I can create the low-level code from other blocks for serial communication, analog input and pulse input/output, and an assortment of 100+ blocks. The speed at which we can complete this project depends on other projects we have underway at Parallax, of course.

Get Me Started with BlocklyProp!

A fast, low-cost way of getting started with BlocklyProp is using the Propeller FLiP Microcontroller Module from Jameco. This 40-pin DIP plugs into a breadboard and has a USB power supply.

Next, create an account and go straight to the Parallax tutorials

Pull out some passive components, LEDs, a speaker and some sensors and you'll be on your way. Or, if you want an all-in-one solution with parts that accompany our tutorials, get the FLiP Try-It Kit.

Propeller Try It KitPropeller Try It Kit

Call the Educator Hotline for help

Parallax is making it really easy for educators to get started. In fact, we're training 500 teachers for free this year - and they each get a robot. These courses are already 80% full, but this doesn't mean you're locked out. Rather, we have another direct solution to support you - the Educator Hotline! If you're a teacher, feel free to give us a call at (916) 625-6801 and tell us about your needs. We've got some webinars scheduled, and we can always get you started using Hangouts or even in person.

Product Links on Jameco:

ActivityBot 360°
QTI Line Follower Kit
ColorPal Color Sensor
Propeller FLiP Microcontroller Module

Additional Resources:

BlocklyProp code entire listing from the project shown above
Line Following with ColorPal and QTI Sensors Complete Tutorial
BlocklyProp Reference Guide
BlocklyProp Tutorials