Motor Controller Mini-workshop – Applying PWM for Speed Control

Last Tuesday we had a mini workshop on controlling DC brushed motors using an Arduino.  We covered controlling the speed and direction of a DC motor (in both directions) using an H-bridge. Some important take-aways from this are:

  • The motor cannot be connected directly to the Arduino, motors typically require more current and voltage.  The ATmega328 (the chip inside the Arduino) can handle a maximum of 40mA of current.  Motors (even small ones) pull 100mA to 10’s or 100A.  If this much current was pulled from the processor it would likely blow up the output port.
  • Approximated to the first order, the speed of a motor is porportional to the voltage applied to the motor.
    • This is a very first-order approximation, the speed will change when a load is applied to the resistance drops in the drive circuitry and the motor.
  • Approximated to the first order, the load on the motor (how hard it is pushing against a load), is proportional to the current through the motor.
  • In order to change the voltage on the motor, a voltage is switched on/off quickly at a certian duty cycle (on some of the time, off the other).  This is known as PWM (pulse-width modulation)
  • On an arduino, the PWM functions are called using the functions “analogWrite”

Example code from the class is:

 void setup() {                
   // initialize the digital pin as an output.
   pinMode(3, OUTPUT);
   pinMode(5, OUTPUT);  
 // the loop routine runs over and over again forever:
 void loop() {
   digitalWrite(3, LOW);   // turn the LED on (HIGH is the voltage level)
   analogWrite(5, 200);
   delay(5000);               // wait for a second
   digitalWrite(5, LOW);    // turn the LED off by making the voltage LOW
   analogWrite(3, 200);
   delay(5000);               // wait for a second

PDF Presentation: Controlling_DC_Brushed_Motors_Using_Arduino

Schematic of the circuit we breadboarded: Motor_workshop_1_Schemtic

from on November 2nd, 2013Comments0 Comments

The Braaain 1.0

Halloween time again. Last year I tried to do a simple little hardware project to make my emoticon pumpkins glow. That’s cute and all but not very difficult.

This year, I decided to work on this idea I’ve had for more than a year and a half. The Brain – a silicone based brain with controllable LEDs inside. I have some ideas of what to do next with it, but this first iteration is just to be a fun decoration for Halloween.



Why The Brain

Back when Interlock was moving into their current space, there was this cool area in the center of it, that was surrounded by windows. That turned into the network room and I really wanted to use that window for something. Show something cool in the window or whatever. I came up with this idea that I would have a brain to represent network activity. When a host goes down, the brain reflects that. If there was no Internet connectivity, the brain would show that too. The first version of the brain is not to that level yet, but it’s in the right direction.

Brain 1.0

Brain 1.0 is a Platsil GEL-10, silicone brain with a hollowed center. In the center is a plastic project box housing an Arduino with Neo Pixels attached to it. A Neo Pixel is an Adafruit project that is meant to be a low cost, multi-color LED that you can daisy-chain, or string together in-line. There’s really no reason to use Neo Pixels for this project besides the fact that I had some already.


  • Halloween Brain Jello mold from Amazon
  • Platsil GEL-10 from BITY Mold Supply
  • Tupperware container donated to the cause
  • XL Breaking Bad meth making gloves
  • Mixing containers

Making the Brain:

This was the most interesting part to me. I picked up the type of PlatSil that is a 1 to 1 compound either by volume or by mass so I didn’t need to worry about mixing too much. I took 500ML of A and mixed it with 500ML of B. This stuff has a 6 minute lifetime from the time you start mixing to the time it starts to harden. There are ways to slow this down, but again, I didn’t need to do that. I spent 2 minutes mixing because some guy on YouTube said this is important, and my recent adventures in Thermite taught me the lesson that they’re serious.

Before I poured it in, I used a can of  Pol-Ease 2300 release which is used to keep the brain separated from the Jello mold. I was reminded the hard way what happens when you forget this. Pouring it into the mold was pretty simple but I made a small clay holder for it so I could make sure it stayed level. After the contents were dumped in, I sunk the plastic project container that was going to be my hollowed inside.

The whole things hardens within 30 minutes but because mine was in the garage in October, it was more like an hour.

Because this stuff isn’t very cheap, I did a demo mold just to make sure I was on the right track.


PlatSil Gel-10:

My goal was to create a mold of a brain that was rubbery and brain like. This Platsil line of chemicals are designed to create molds for other things. There wasn’t a lot of people making actual things from the material itself but I really like the texture and toughness of using it as the model. I will say that it is 100% overkill for what I wanted. There’s probably someone that can recommend a better, cheaper, alternative but for me this worked in the time frame I needed it to. They have a bunch of different types and I really wanted light to diffuse through it so I got that translucent version. It still comes out pretty white depending on how thick of a mold you’re making.


Neo Pixel:

Neo Pixels are really slick. They have 4 leads on them. Power, Ground, signal in, and a signal out. The biggest benefit is that each pixel is individually addressable without the need for multiple connections. Pixel 0 connects to pixel 1 that connects to pixel N through a single  wire connected to your microcontroller or whatever you’re using.

Power takes +5v, and there is a warning about memory consumption especially with smaller Arduinos and extremely long chains of Neo Pixels (up to 500 at 30 FPS). My 4 didn’t mind.

Adafruit has a Neo Pixel library that you can use pretty easily, even if you just want to hack one of their demos.



This is my hacked code to make the brain throb between red and pink. Again, a Neo Pixel is overkill for doing this but it’s fun none-the-less and I’ll be upgrading it next iteration.

#include <Adafruit_NeoPixel.h>
//Hacked from the original Adafruit library demo

#define PIN 6   //my control pin

// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  strip.begin();; // Initialize all pixels to 'off'

void loop() {
  //Start out with a pink brain looking color
  colorWipe(strip.Color(255, 48, 48), 1); // Hot Pink

  //Throb read and then fade out

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, c);;

void rainbow(uint8_t wait) {
  //secret rainbow mode
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));

void heartThrob(uint8_t wait) {
  uint16_t i, j;

  //Adjust 60 and 90 to the starting and ending colors you want to fade between. 
  for(j=60; j<90; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 85) {
   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if(WheelPos < 170) {
   WheelPos -= 85;
   return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);

from on November 2nd, 2013Comments0 Comments

Raspberry Pi-ing, MacGyver Style

Ok, so there’s no duct tape, toothpicks, or bubble gum involved, but I thought it’d be helpful to document a handy way to tinker with your Raspberry Pi, even when you’re almost completely lacking in the required external paraphernalia.

Raspberry Pi Workshop

We recently had Rob Bishop from the Raspberry Pi foundation stop by Interlock and give a workshop for a couple of hours, and as we scrambled to prepare — attempting to pull together 20 hdmi monitors, 20 usb keyboards, 20 power supplies, et cetera — I kept thinking that it’d be nice to be able to play around with a Pi even if you’ve arrived unprepared for such an event. So what follows is “how to fiddle with the Pi GUI with just your laptop, an ethernet cable, and a micro USB cable”. We’re going to start with a freshly imaged SD card, hook it up to a Mac or Ubuntu laptop through their internet sharing capabilities, and use VNC to share the GUI over the network.


Getting Started

The first step we’ll gloss over is copying a fresh Raspbian image onto an SD card. This is covered in great detail at the embedded Linux wiki, where they have plenty of information on setting up the Raspberry Pi. Go there, follow along, and insert your newly imaged card into your Pi.

Internet Sharing

Next, we’re going to prep our laptop to share its internet connection with the Pi. I know nothing about Windows, but on Ubuntu and Mac OSX, there are easy ways to share your wifi connection with any computers that are hooked up to your ethernet port. We’ll take these one OS at a time:

Screen shot 2012-12-05 at 4.25.15 PM

Assuming your Mac is connected to a wireless access point, open up the “Sharing” control panel in System Preferences. Click on the “Internet Sharing” option, which is probably grayed out at this point. Choose “Ethernet” as the port you’ll share your internet connection out of, and then turn on Internet Sharing by clicking its checkbox, which should now be clickable. Done.

Screenshot - 12052012 - 07:15:23 PM

This step is equally easy on Ubuntu (and its variants… I use Xubuntu). Open up the “Network Connections” system preferences. You’ll see something like the above screenshot. Click the big “+ Add” button.

Screenshot - 12052012 - 07:16:31 PM

You’ll be confronted with something like the above. Name this new connection something logical. “Connection Sharing” works for my brain. Click over to the “IPv4″ tab and select “Shared to other computers” from the “method” dropdown. Some people have reported problems with IPv6 at this point, so click over to that tab and select “Ignore” in the same dropdown. Save the connection. Carry on.

Plugging In

Now we should be back on the same page for both OSes. Find the shortest ethernet cable you can and plug your Pi into your laptop’s ethernet port. Nothing should happen. Plug the USB cable into your laptop, and then into the micro USB port on the Pi. Before doing so, reflect upon the fact that the Pi foundation does not recommend you power your Pi off of a computer’s USB port. Your computer might not be able to supply enough power, and/or might burst into flames. I’ve not had any such problems, but if your Pi is acting flaky, or if you’re planning on plugging any USB peripherals into the Pi, you may want to sacrifice MacGyver points and portability in favor of a wall outlet.

Sleuthing IPs

Ok. We’re plugged in with internet tubes and power. Your Pi should be booting up and making happy blinkies on its status LEDs. After a short while, it’s safe to assume that it has fully booted and grabbed an IP address from your laptop. Hooray! Open up a terminal and perform the following incantations to find out what address it was assigned.

Screen shot 2012-12-05 at 6.12.15 PM

On the Mac, cat /private/var/db/dhcpd_leases should pop up a block of text with the desired info. Above, we got an address of That’s probably pretty standard.

Screenshot - 12052012 - 07:23:02 PM

On Ubuntu Linux, cat /var/lib/misc/dnsmasq.leases will get you the same info. In this case, our IP is

SSH, and Setting Up a VNC Server

Just a few more steps. We need to SSH into the Pi. That is, we need to connect to a command shell on the Pi so we can install a few things to get our display up and running.

ssh pi@x.x.x.x

Substitute the IP you found previously for all the x.x.x.x mayhem at the end there. It should connect and ask you for the default password, which is “raspberry”. Now install the vnc server. This is what will transmit the desktop environment to your laptop:

apt-get install tightvncserver

Run the VNC server once, and it will ask you to set a password:


Set and remember a password when it prompts you. I just used “raspberry” again. I’m creative like that. Now we’ll run the VNC server for real:

vncserver :1

The “:1″ is the display number. This is necessary because you could serve multiple virtual displays off of one computer. But lets not get ahead of ourselves.

Installing a VNC Client

We’re now ready to install the VNC client on our laptop and connect to the Pi’s GUI. Chicken of the VNC is a popular Mac client. Download and install that as per normal. On Ubuntu, Vinagre is probably the most popular client. apt-get install vinagre should get it installed without a problem.

Screen shot 2012-12-05 at 4.52.36 PM

Almost there. Above is the Chicken of the VNC connection dialog. Fill in the IP address you found previously, and the “1” display number and password. Click connect and you should be up and running! On Ubuntu, the command vinagre x.x.x.x:1 will do the same thing. Yay! You’re done. Celebrate your hard work and dedication while admiring your glorious accomplishment:


Extra Credit

Fellow Interlock member BleuLlama suggested a way to make this process a bit easier the second time around. Rather than hunting down the Pi’s IP address (which is admittedly not very difficult to do), we can use Apple’s Bonjour protocol (aka ZeroConf) to have the Pi announce itself to the network. I found a good writeup on how to do this, plus how to get the VNC server to start automatically on boot. Check it out at Pat Galea’s blog. You can ignore the part about installing Netatalk unless you care about Mac file sharing.

When you’re done with your extra credit, you’ll have a Pi that you can plug into your ethernet and USB ports, wait a minute or two, and it’ll pop up automatically in Chicken of the VNC or Vinagre (or any Bonjour enabled VNC client, of course).

from on December 6th, 2012Comments17 Comments