Embedded Programming
(Intro to Arduino)

"The Analytical Engine has no pretensions whatever to originate anything. It can do whatever we know how to order it to perform... But it is likely to exert an indirect and reciprocal influence on science itself." --Ada Lovelace (inventor of programming, 1815-1852) quote found on BrainyQuote

What is Arduino?

"Arduino is an open-source electronics platform based on easy-to-use hardware and software. Arduino boards are able to read inputs - light on a sensor, a finger on a button, or a Twitter message - and turn it into an output - activating a motor, turning on an LED, publishing something online. You can tell your board what to do by sending a set of instructions to the microcontroller on the board. To do so you use the Arduino programming language (based on Wiring), and the Arduino Software (IDE), based on Processing."

-- Paragraph above borrowed with love from Arduino.cc


“Talk is cheap. Show me the code.”― Linus Torvalds

Fab Academy:

These are in-depth discussions of topics related to this unit, borrowed with love from the Fab Academy archives. The videos linked here (and on the rest of the webpage) are intended to deepen your knowledge on the topics. They are not required for the Foundations of Fabrication course, but they are often good to watch for more perspectives on the same ideas. We love 🎧 listening to videos while doing the repetitive tasks of making; 👀peeking over whenever they've got something good on screen.

This unit will need to be differentiated. For those students that have never coded anything, following the standard ‘LEarn to Code on an Arduino’ will be more than enough. For students who are already proficient with Arduino, this should shift focus to direct them toward moving beyond their need for Arduino hardware, and then moving beyond needing the Arduino IDE.

Unit 11.A =====

U11.A.1 Learn to code on an Arduino

Slides on an Introduction to Arduino

  • Scopes lessons 1-5

U11.A.2 Learn to code on an Arduino (continued)

Slides on More Advanced Arduino Concepts

Scopes lessons 6-10

So, there will be some of you who already know how to code at this point. For those of you, we will go down a different rabbit hole: programming AVR chips in and outside the Arduino framework. For many simple electronics projects, it turns out the 1) the Arduino Uno is more powerful than the project needs and 2) you can reduce your overhead/ improve your performance if you skip the Arduino bootloader. These are advanced moves, and should only be taken on by people who already know what they are doing with programming and troubleshooting an Arduino.

The Arduino IDE and corresponding boards are brilliant in that they abstract away so much of the needed skill. The first time (or even advanced) user doesn't need to be aware of all the things the IDE does in the background to make the program get to the pins of an Arduino. Really, it's incredible!

If you're looking to go deeper with Arduino, then I would recommend you skim through this series of pages from Adafruit.

Unit 11.B.1 =====

Programming a bare AVR chip

This week we will focus on removing the hardware needs of the Arduino platform, so that you can build microcontroller projects much cheaper and with far fewer parts. A simple AVR chip will cost less than $2, even when they are not purchased in bulk. Sure, it does take some extra work to get to the point where you can pre-program and test bare chips, but it can be worth it if you intend to advance your game or sell products that use them. But before we go too far down that path, let's just come up with a plan for this week:

  1. set-up a normal Arduino Uno for the Arduino-as-ISP example sketch located in the Arduino IDE at: File > Examples > ArduinoISP > ArduinoISP

  2. get your chip set-up on a breadboard. Here are two articles that explain how to do this:

  3. Find the datasheet for whichever ATtiny you are using, and feel a bit sad about how long it is. Then move on to finding a pinout diagram like the one HERE, HERE or HERE. (Just a note of caution: Pinout diagrams such as these are often notes made from a full datasheet.)

  4. If you've followed the instructions, you should burn the bootloader to the ATtiny chip (or a loose ATMega328P) so that it is ready to accept arduino sketches as usual, but through the ArduinoISP.

  5. Write a small 'hello world' sketch for your new chip, where you do something simple: drive a single pin HIGH and LOW, with the basic blink sketch. You'll need to modify the LED_BUILTIN to a pin number your particular chip has available.

  6. You'll be able to verify with a multimeter or LED/resistor that this is working, and the chip is able to actually receive code from the IDE.

  7. From here, write a sketch you want, assemble things on the breadboard, and remove the ISP connections (except for 5V and Ground).

If you've made it this far, you have successfully programmed a 'bare' chip using the bootloader and can now make all your Arduino-like projects much smaller and cheaper! If you want to go real deep: This can be expanded to work for SMD chips either through pogo pins (like the Sparkfun article above) or the slightly easier alternative of just leaving a header available in your circuit design, so you can get the ISP programmer connected.

Program a bare AVR chip (any kind) using the ArduinoISP or any other ISP programmer. For this week, it is recommended that you stick with the Arduino bootloader. Bonus points if you build this programmed chip into a circuit that runs on a battery, independent of any computer or other Arduino stuffs afterwards.


Lecture

Unit 11.B.2 =====

Moving past the Bootloader

So, you've gotten a chip to work on it's own. Awesome! Now it's time to learn what deep earth magic is at play to get from the code you write in a sketch, to the behavior your arduino takes. There are plenty of good tutorials for this on the web, you can check out HERE and HERE.

The Arduino IDE does lots of things for you, and we will break some of those down here:

  • Arduino installs a bootloader on your Arduino-compatible chips. [We did that last week in step 4, since our chip didn't have this pre-done.] The bootloader is a special program that is preloaded on any official Arduino product that lets the chip receive new programs over serial communication. This is the main reason why it's good practice not to use digital pins 0 and 1 on an Arduino Uno - they are the ones that 'listen' for reprogramming commands over USB. That said, the bootloader has some awesome benefits: 1) you can program an Arduino over cheap/ available USB 2) the typical arduino user doesn't need to know about 'fuses' on the chip, or many other features. There are also some drawbacks to the bootloader: 1) it takes up some of the chip's memory 2) it locks the clock speed of the chip and a few more things...

  • The Arduino IDE converts your sketch from Arduino's version of C++ to Hex code (a binary varant). This is important, because the AVR chip only understands Hex. This is what all IDE's do for programmers when they compile the code. The value judgements implicit in the subtleties of this process are hotly debated among the nerdiest of programmers. ❤️

  • Arduino auto-generates a "Makefile" for you, which is a set of instructions for going between the code you wrote and the chip itself. These are actually findable in the Arduino IDE, but they can be written by hand as well, if you're trying to avoid the Arduino IDE all together.

  • The Arduino-generated Makefile does the tedious work of changing what a programmer specifies as pin 1 or A0, into the 8-bit register address of the pin. Since each Arduino board is based around a specific chip (and you specify the board type in the IDE) the makefile takes care of going from Arduino's pin name to the specific addresses on that particular chip/board. This means Arduino users don't need to be aware of 8-bit pin registers.

  • There are several parts of Arduino code that are not standard C++ and were added for user convenience. For example, the words HIGH and LOW refer to the electrical state of pins, and would not be in C++ itself by default. The Serial object [and many more] are not a part of standard C++ that you would find outside of the Arduino environment. These have been added to make life easier for anyone new-to-Arduino.

It is possible to write your own code, compile it, and move it onto an AVR chip all without ever using the Arduino IDE. In most projects, this isn't necessary, but it can be a way to squeeze a little more out of a chip or project. We're going to address a few scenarios where this could be useful:


Program doesn't respond fast enough? Try using your Interrupt Pins!

A former student needed to build a car that could measure how far it had gone in a straight line. This is actually not too complex, if you can keep your wheels from slipping. Just tie a quadrature encoder to one of the wheels, and you can measure the rotation and direction. From there, you do a little calibration and you can do dead-reckoning with often sub-millimeter accuracy!

The hard part is that most cars + drivers like to go fast. If the encoder spins faster than a program can read the pins of the encoder, you'll have some problems. Missed steps in the encoder can mean misestimating distance. A good solution to this is to use the special 'interrupt pins' on the chip/Arduino. These are so-named because they can be programmed to interrupt the running program on the chip and execute a short bit of code before returning to the regularly scheduled program. This works great for an encoder, because you can use the interrupt pins to ensure the encoder is read as it turns (as a priority) and the program to respond happens in the background.

PJRC has built a nice Arduino library to make this easier for first-time users.

Want sweet freedom from Arduino? (Who we ❤️ and will miss...)

It is possible to build your own dedicated ISP, as shown HERE, HERE and several other places around the web. Once you've made that little friend, you can go about programming your own chips with a process that completely circumvents the Arduino hardware or IDE. You can also buy an ISP for pretty cheap, if you don't feel compelled to have made it all on your own.

Here is an article on programming an arduino in pure C. Which can be rewarding but tricky... and here are a few examples for an ATtiny44 and makefiles to get you started programming without the Arduino IDE. But a word of caution: this can be finicky and need edits or supplemental programs installed (such as AVRdude, Make or AVR-gcc). It may be best to buy a whole book...

Another option that is possibly more beginner-friendly than the one above is to program a board with CircuitPython. Adafruit has started this environment, but it has enjoyed a growing community. Here are some instructions for setting up a board and getting started programming in python. The catch here is that AVR chips don't seem to work - You'll have to switch to SAM or SAMD boards. A common example is the Micro:Bit, designed to go with England's Python-centric initiative to teach students how to code. With a certain bootloader-like program on a compatible chip, you become able to run python programs directly. This may be the easiest way to be done with Arduino. There are also some great guides out there to more completely help you migrate your skills to Python from another language.

There are many types of microcontroller chips: AVR, SAMD, and ARM are all options. Each with their own quirks. Moving into this territory where you program them without the aid of the Arduino IDE can be tricky, but often rewarding. It's also often the key to maximizing profits...

You've got options here, based on how adventurous you want to get:

  • Easy-ish: Write and use an Arduino sketch that uses the interrupt pins successfully

  • Easy-ish: Program any kind of board using CircuitPython

  • Not-Easy: Build and test your own ISP programmer

  • Not-Easy: Program an AVR chip with pure C, following the instructions linked above and any ISP programmer

Lecture