Posted on

The Best way to program an Arduino Microcontroller from the Linux Command Line Interface

Updating arduino code over the internet hasn’t been easy for me before now. In this post I will show you how to update 200+ different microcontrollers from the Linux Command Line Interface (CLI) using PlatformIO. My specific example (see instagram video below) will be a Teensy 3.2 using a Raspberry Pi, but you can repeat it for many other combinations with a little effort.  This method is specifically for the times when you have no Graphical User Interface (GUI) What-you-see-is-what-you-get (WYSIWYG) into the remote machine such as TeamViewer and all you have a bare bones text interface to the remote device.

Continue reading The Best way to program an Arduino Microcontroller from the Linux Command Line Interface

Posted on

How would I create a robot arm from scratch?

Reddit user singdawg asked a great question:

I’m currently starting a project to [create a robot arm]. Anybody know good resources? I’m a capable programmer, have experience with microelectronics and microcontrollers. Have some advanced maths to understand the depth of material (jacobian matrices etc) but I’ve limited experience with servo motors.


If your goal is specifically to DIY a robot arm from scratch, I’d start by figuring out what are my design constraints. I’d choose a carrying weight limit, a reach distance, the number of DOF, and the maximum mass of each joint. from that I could build a kinematic chain to find my torque limits, and match those to available motors and gearboxes. Personally, I choose stepper motors. In Fusion360 I’d create models of those motors, then start placing them at their desired locations. The gaps between parts would be filled with my custom designs, which would then have to be manufactured. The custom design part is very iterative and very slow (for me). Some of the things I ask myself are: Where do the bearings and fasteners fit? How do I plan to make this custom part and how does that affect the design? Where do I run the wiring so it doesn’t catch and break? Have I remembered to put in *every* part, not just waved my hand and said “figure it out later”? Can I design this in smaller pieces for easier testing of each piece?

I should mention here that a robot built with stepper motors can’t tell where it is from moment to moment the way you or I can. Mostly they are built by having limit switches. The robot moves to touch the switches at startup. Since it knows where the switches are it can count steps as it moves from then on. It is crucial from that point on to never miss a step. One day we’ll have better encoders for DIY robot arms, but not yet!

Once I’ve reached a design I like, it’s time to use the bill of materials from Fusion360 to place my order for the parts, and get to making the custom bits while the orders are in the mail. Once they arrive I can put it together and figure out what I did wrong, then go back to the fusion custom part step 🙂

Once I get something that doesn’t fail on assembly, I take each major section of the arm in Fusion360 and save them out as STL files. I bring those into Robot Overlord and make it move virtually. I can then modify some Arduino CNC firmware to follow the same kinematic model that I used in RO, and now I have a GUI to drive it. There are several arms already in RO, feel free to branch it and add your own.

Robot Arm Torque Calculator

I find calculating forces boring and I love to code. So I wrote a Processing sketch that can simulate a robot arm enough to calculate some masses and torque values. My thinking is that I can use this to set an upper limit on the weight of each joint, then see the torque values and find the motors that will be under-weight and over-torque.

Robot arm torque calculator

The arm can be moved by clicking on a joint and pressing Q/E.  The values at the bottom are the joint number, the direction of rotation, the maximum weight, the distance from the previous joint, the current angle, and the current torque.  In a 6DOF arm there are joints 0-5 and joint 6 represents the weight of the tool or the payload carried by the arm.

Get and run the Arm torque calculator.

Final thoughts

I’ve done this with 3dof and 5dof arms. I’m currently working on a 6dof robot arm.  I like to design from the wrist backwards, because the payload is the most important part, and each motion after that depends on the ones that come before it.

I’m constantly distracted by the work of assembling my other machines. Ironic! If I had the arms they would do the work for me. Soon, soon!

Next in part 2 I will show some of my work designing the arm based on the calculated constraints.

Posted on

Animated Carnival Light Arrow Upgrade

Paul came to the shop with a 2′ carnival light arrow sign that had five LED light bulbs inside. He asked if I could make them light one at a time to animate the pointing effect. Read on to see how it was done.

Continue reading Animated Carnival Light Arrow Upgrade

Posted on

Build your own falling block game like Tetris

I’ve shown you how to use shift registers to drive an LED grid, including how to draw pictures on the screen from memory. Now we’re going to use those tools to make a game similar to the classic Tetris.  I’ll show you the circuit, how to draw pieces, how to create animations, respond to user input, and more.  Learning how to build complex behavior from simple parts is a great start to thinking about how robots behave.

Continue reading Build your own falling block game like Tetris

Posted on

How to use an 8×8 LED grid with Arduino

In recent posts I’ve covered how to use LEDs and how to use shift registers and even how to combine shift registers and LEDs to control numeric displays.  In this post we’re going to use 64 LEDs in an 8×8 LED grid.

ELEC-0082 LED grid red

Continue reading How to use an 8×8 LED grid with Arduino

Posted on

Shift registers and seven segment displays in Arduino

In previous tutorials I showed how to use seven segment displays, how to use shift registers and how to daisy chain shift registers, and talked a bit about persistence of vision.  In this tutorial we’re going to combine shift registers and seven segment displays to make an Arduino control two four-character seven-segment displays, a task that (at first glance) might not seem possible.

Continue reading Shift registers and seven segment displays in Arduino

Posted on

How to daisy chain 74hc595n shift registers

ELEC-0116 shift register

In the previous post we learned how to use a 74hc595n shift register.  I mentioned briefly that shift registers could be daisy chained – connected end to end – to increase the number of available connections without using extra resources on the Arduino.  In this post we’ll cover why daisy chain shift registers, how to plug them together, what changes in the code, and show some examples in a video at the end. Continue reading How to daisy chain 74hc595n shift registers

Posted on

How to use a 74hc595 shift register

ELEC-0116 shift register

Using a shift register is a super easy way to expand the number of electronic components connected to an microcontroller like an Arudino or a NodeMCU.  We’re going to cover what is a shift register, how shift registers works in theory, how to connect a shift register, and then look at some practical programming examples.  Follow along with the helpful diagrams and you’ll have it mastered in no time!

What is a shift register?

A microcontroller gives me the power to change voltage in a circuit using code. An Arduino microcontroller has 13 digital pins that I can control individually. BUT! Sometimes I need more than 13. Instead of buying bigger and bigger microcontrollers I can use a shift registers. One shift register will let me control the voltage on eight pins and only uses three pins on my microcontroller. Where it really starts to get exciting is when I daisy chain the shift registers – two daisy chained shift registers can control 16 pins and still only uses three pins on my microcontroller.

How does a shift register work?

shift register functional block diagram

To understand how shift registers are used, it can help to understand what’s going on inside the chip.  This is a functional block diagram of the parts inside a shift register package. Each block represents a sub-circuit called a latch.  Each of these latches has a power in pin, a set pin, a data pin, and an output pin.  When the set pin voltage goes from low to high the latch saves the low/high state of the data pin.  The saved state in the latch is always being transmitted on the output pin.

The latches down the left side are chained to each other in series.  When the voltage on the SRCLK pin changes from low to high, the latch will store whatever is output by the previous latch and the first latch will store whatever is output on the SER pin.  Every time SRCLK goes high the contents of the latches moves over by one latch.  If there’s a shift register B attached to this shift register A, B-SER (pin 14 on the DIP 16)  can read from A-QH’ (pin 9 on the DIP 16).

So far this solves one challenge (connect many things to a few pins) and introduces a new one:  What happens if you only want to change the voltage on, say, pin 5?  If you try to shift in 8 new values, each SRCLK high/low might change the output of latch 5 in ways you don’t want.  There needs to be a buffer, a way to say “I’ve shifted in all my new data (with only pin 5 changed), now change the voltage once on all the pins coming out of the shift register.”

The latches on the right hand side have their data pins connected to the latches on the left.  The latches on the right have all their set pins connected to RCLK.  When RCLK goes from low to high the output of all eight of the latches on the left are copied into the latches on the right and the output pins change once.  Nice!  Problem solved.

Lastly, there are two more features that make this shift register extra excellent:  First, while the OE pin voltage is high it will turn off all the outputs from the eight shift register pins without losing any saved states in any latch.  Second, when the SRCLR pin goes high it will wipe the memory of the left-side latches without affecting the right-side latches.  Depending on what you’re doing they can be very useful.

So now that we understand the theory of how these work, let’s get down to wiring and then some practical example.

Wiring a shift register to an Arduino

I’m going to connect an Arduino to a shift register to eight Light Emitting Diodes.  The LEDs are to show the changing voltage on each output pin of the 74hc595n shift register.  I googled for a shift register data sheet and used the pins out instructions to connect a shift register to an Arduino with a breadboard and some wires. Here’s the the shift register, a standard DIP 16 package:

That the bump on the end of the DIP 16 tells you which pin is which.

74hc959 pins top viewNotice the numbers and names on this picture match those in the functional block diagram seen in another part of the data sheet.

Here is a schematic diagram of a shift register connected to an Arduino that I made in KiCAD.

shift register kicad schematic layout

On the left are the pins from the Arduino.  In the middle is the shift register.  On the right are eight LEDs and eight 220 ohm resistors.

I don’t know why the pins on the shift register are out of order.  KiCAD has an eccentric idea of “easy”An LED without a resistor is a dead LED.

Here’s a picture of the same circuit I built on a breadboard:

An arduino connected to a shift register connected to eight LEDs.

A post shared by Marginally Clever Robots (@imakerobots) on

Please let me know if you are struggling to make a circuit with this information.  I’m here to help.

All together now!

Circuit is built and now I’m ready to test.  I’ve put all my shift register code on github for you so you can follow along.  There is a single file called one-shift-register.ino that you can load into Arduino software.  I assume you’ve used Arduino before and can work your way through code.  The main loop() has all our favorite examples.

lightFirst() – light just the first LED.
lightAll() – light all the LEDs.
blinkA() – blink all the lights at once.
blinkB() – same effect, done a different way.
blinkC() – same same but different.
oneAtATime() – light the first, then the second, and so on.
pingPong() – the lit LED appears to move backward and forward.
pingPongSine() – the ping pong effect has a sine curve, so it moves fast in the middle and slow on the ends.
bouncingLevelA() – lighting some of the lights from one end light the levels on a sound board.
bouncingLevelB() – same again, from the other direction.
marqueeA() – the moving lights around the edge of a Broadway show marquee.
marqueeB() – same, different style.

To keep things clear, I give every little job a human-friendly method name.

turnOutputsOn() – turn on the output pins of the shift register

turnOutputsOff() – guess!

clearShiftRegisters() – wipe the stored value in the left-side registers (you didn’t skip “How does a shift register work”, right?)

shiftDataIn(int data) – set the SER pin, then flip the SRCLK pin to high and back to low.  all the left-side latches move over by one and the data is now in the first latch.

copyShiftToStorage() – copy the left side latches to the right side latches.

BAM!   So good.


Final thoughts

Yes, in this demo I am using five wires.  In many applications you can skip the OE and SRCLR wires.  Some people even wire the two CLK pins together so they only need two wires.

Yes, the pins coming out of a shift register could control another shift register in some kind of tree-of-registers. In find it easier to daisy chain them.  You do you.

Yes, there are many other kinds of shift registers.  This is my favorite.

There are many more kinds of latches than discussed here.  When you know how latches work, you can start to understand how computers, y’know… compute.

I’ve designed a circuit board that you can solder together so that you won’t have loose wires all over the place.  Would you like one?

3d preview of one-shift-register

Comment below and we’ll make it happen.  KiCAD did not include the LEDs or the resistors in the 3d preview.  I think you can tell where they go.  They would be included in the package.

See Also