Maybe you’ve been using codebender for a while and have had great success making things. You write the code, click the magic ‘Run on arduino’ button and a few seconds (and some blinking LEDs) later your board is programmed and you enjoy the fruits of your efforts. If you have ever wondered how that generated machine code gets its way into your board, this article is for you.
Traditionally, to access hardware in your computer (like your board) you needed desktop applications like the arduino IDE. You had to download them, set them up and if you owned more than one computer, repeat per computer syncing changes between files yourself. Same with any extra library you might need. This tends to become cumbersome after a while and since almost no computer is exactly the same, sometimes causes issues that are too complex to figure out yourself.
Fortunately codebender runs through your browser. This means you immediately benefit from using the cloud for storing your sketches, compiling and even accessing a myriad of libraries. The downside is that the browser stands in the way of any web application (like codebender) accessing your boards. So how does codebender do it?
Luckily all modern browsers support some way of extending them via plugins/extensions (the naming doesn’t really matter). At codebender we currently support two modern browsers, Firefox and Chrome. Chances are you’re using one of them to see this article now. A little while back both where using the same technology but about a year ago, Chrome switched to a new way of doing things. You don’t need to know details but that explains why sometimes things work slightly differently between the two browsers. The plugin (for Firefox) or app extension (for Chrome) that we ask you to download during your first visit give codebender access to your boards.
Once your code is compiled and ready a process called ‘uploading’, ‘programming’ or ‘flashing’ needs to begin. Our site notifies the plugin/app and passes in all necessary information; the machine code that will be uploaded, the type of programming protocol, the connection port to be used and any other parameters that might be needed, like the programming speed.
If you remember from an article a little while back, the brains of your board is a microcontroller.
In the past, to program the microcontroller with new code you needed a specialized hardware programmer connected to a special header on the board – or even directly on top of the chip to be programmed – and then the code was transmitted down to the chip using electrical signals on obscure manufacturer specified protocols.
These programmers were meant to be used by engineers and therefore needed specialized software and some expertise to get them working. Needless to say that most of the time they were quite costly.
Fortunately almost all recent microcontrollers have access to their internal program memory and the ability to reprogram themselves from already running code. You only need something in the chip to be able to tell when the already programmed code should run or know how to program the device with the new code coming in. That’s what the bootloader does!
Each arduino board comes out pre-programmed with a bootloader. When you first power your board, the bootloader is the first code to execute. On start-up it will try to figure out if the computer is asking for new code to be programmed and if not, execute the previously stored code. That simple!
If the bootloader detects new code waiting to be programmed, it will start re-writing the program memory with the new content. The benefit is that we can utilize the serial connection that was previously used for the serial monitor to also program the device. No special hardware is required anymore; not even a second port, neat!
Now that we know the other part of the equation, it’s kind of easy to imagine what’s coming next in the flashing process. The codebender plugin/app will try to get the board to programming mode by first resetting it and then sending in the special data the bootloader is looking for. Then, when it sees a proper reply from the bootloader, starts sending in the data, waiting for confirmation from the bootloader that all went well. At the end, one more board reset will execute your newly stored code.
Not all boards are programmed with the same programming protocol. Some require special handling like confirming the data being written or expecting certain responses in certain timings. This means that programming the board can fail from time to time, but usually it’s no more a hassle than pressing the ‘Run on arduino’ button once more. If you keep getting errors then it may be a sign that something’s wrong. Either your board or port was incorrectly selected (which means the plugin/app and your board can’t understand each other’s “language”) or it’s perhaps not properly connected (checked with another USB cable?).
So next time you press that ‘Run on arduino” button, step back and admire the technology that is working for you!