UART Transceiver for breadboard computer

You art UARTI have an increasing fascination with building 8-bit computers from TTL chips, sparked by the excellent Ben Eater series on YouTube, and deepened by a variety of examples on Hackaday.  In the process of designing/building my own 8-bit computer, I wondered how easy it would be to implement a UART transceiver purely out of basic 7400 series ICs...
The finished design: UART Transceiver built from 7400 series ICs.

SN76489 Arduino MIDI player

I've been thinking about retro 8-bit computers recently and came across the audio chip SN76489 (datasheet), which has been used in the BBC Micro, Sega Genesis and Master System (among many others).  The chips are capable of generating 3 independent frequencies simultaneously, as well as a 4th channel for noise.  They seemed easy enough to interact with, using a write pulse to load commands into the chip in a parallel-fashion.  I thought it would be fun to hook one up to an Arduino and play some retro game MIDI files through them!  It would be easy to take a few and make a MIDI synthesizer out of them too.

All code for this project can be found at my Github here:
It's easy to pick up a few cheaply from the usual sort of places, so I bought a pack of 5, and then had a read through the datasheet, which is always fun!

Assembler for Ben Eater's 8-bit computer

I've been thinking about 8-bit computers recently and came across the most excellent breadboard 8-bit computer series by Ben Eater.  Absolutely terrific stuff, fantastic break down of the subject and a clear and talented educator.  Like many, it got me pondering building my own 8-bit computer, modifying the original design to my specifications and writing custom opcodes and microcode.  Thinking about how to convert assembly language easily to machine code for such a custom computer, I wondered if there were any assemblers out there for assembling custom ASM.  It turns out there are, and the one I've been playing with is called... customasm.

EAGLE useful tips

Eagle is a pretty useful piece of software for designing circuit schematics and PCBs.  I don't use it often enough to get really good at it however, and there are certain things in Eagle that I find myself having to "google" every time I want to do it!  So without further ado, this is my growing list of Eagle tips, in no particular order:

ATtiny Mario handset

This is part 2 of my ATtiny Mario project - part one is here - a Mario-type game I made for the ATtiny85 AVR as a fun way of learning about the chip.  After writing the software, I decided I wanted a nice housing for the mass of wires on my breadboards and thought it would cool to put it into a NES-style controller, which is what this post will discuss.

ESP8266 low power DHT11 temperature and moisture remote sensor

I have a problem with moisture in my loft.  After planning some intervention, I wanted a way to remotely monitor the humidity and temperature up there, and to visualise the data on a server.

ATtiny Mario

ATtiny85 Mario Challenge! The ATtiny85 microcontroller is a capable but compact AVR.  However with only 8K of program memory, 512 bytes of EEPROM, 512 of SRAM and only 8 pins (3 of which are dedicated to the usual suspects of Vcc, GND and RESET), it best suited to performing tasks where memory demands are not large, and only a few pins are required for interfacing.  

Arduino and Raspberry Pi serial communciation

Want to hook up your Arduino to your Raspberry Pi and send data between the two?  Read on! So you have your Arduino robot, or weather station and you want to send data back and forth with a Raspberry Pi?  The hardware hookup is really easy, but we have to configure a few things on the Pi.
Hardware connections We'll use the serial UART pins on the Raspberry Pi Zero GPIO for this example, and transfer data from an Arduino to a Raspberry Pi zero, though all Raspberry Pis and Arduinos are capable of this feat.  The UART is a common serial communication protocol comprised of two pins labelled, RX (receive data pin) and "TX" (transmit data pin).  Wiring is easy - hook up the desired RX pin on the Arduino to the Raspberry Pi TX pin, and the Arduino TX to the Raspberry Pi RX pin.  As always, make sure you connect the ground line between the Arduino and the Raspberry Pi.  If the particular Arduino model you are using is powered by 5V (find out by measuring the voltage on the TX …

PS2 controller as a radio controller!

Turning a PS2 controller into a wireless radio controller with Arduino Mini and an nRF24l01 radio module
I have previously described a project to transmit PS2 controller signals by radio.  The project featured an Arduino Nano that read the PS2 signals and transmitted them with an nRF24l01 module.  Recently I have been inspired to incorporate the battery, charging circuit and Arduino right into the controller - heavily inspired by this project here:

Using the NodeMCU ESP-12E board with Arduino, MicroPython and Lua!

Many ventures into the world of the ESP8266 chip and making Internet of Things devices would do worse than to start with the NodeMCU ESP-12E board.  I started my journey with the powerful but stripped back ESP-01S board - useful for projects such my IR-Egg and cat litter tray Twitter bot where space is an a premium, and very few GPIOs are required.  However I have recently got my hands on some NodeMCU ESP-12E boards and I'm enjoying the relative abundance of broken out pins, the builtin USB with 5V --> 3.3V conversion and programming over the USB.  So far I have used the ESP-12E boards in a couple of small projects, namely as a SPIFFs HTTP file server and as the reciprocal HTTP client.

Getting started with the Pro Micro Arduino Board

This is a brief guide to getting started with the Pro Micro Arduino Board (Atmega32U4) - not to be confused with the Pro Mini(which utilises the Atmega328P)!  Note that the Pro Micro board comes in two flavours/flavors - 5V @ 16 MHz and 3.3V @ 8 MHz, so make sure you know which you have before getting started.  It should be indicated on the bottom silkscreen of the bottom, or you could take a voltmeter and measure VCC against GND - it will be either 5V or 3.3V.

ESP8266 as an HTTP client

I recently blogged about how to setup an ESP8266 as a file server, in particular using SPIFFs to store the files.  But how can we retrieve HTML and general files from a web server?  In this post I'll demonstrate some code to make a request for a file from a server, then save the results on the SPIFFS on the ESP8266 client.  In terms of implementation, this works very well on a NodeMCU ESP-12E board.  I've also tried it on a ESP-01S board - there is a technical issue writing to SPIFFs within a program on some of the latest versions of this board.  There is a fix however, discussed at the end of this post.

ESP8266 as a SPIFFs File Server

How to setup an ESP8266 board as a server using SPIFFs This how-to guide describes using an ESP8266 chip to setup a server to do what servers do... serve files!  If you want to know how to retrieve files from a web server, see my post on using the ESP8266 as a HTTP client.  Although HTML and other files can be contained within the program itself ("sketch" in the Arduino parlance), it is inconvenient, especially if you want to deposit images on the ESP-board, or change your files in and out easily.  The solution is use to SPIFFs, a file system that uses the on-board SPI flash chip, which can be written directly from your computer (and read and written from programmatically within your sketch too).  The amount of memory will vary depending upon the board in question.

Litter tray TwitterBot - BeemoPoops!

The Internet of cat litter trays - TwitterTray The world almost certainly doesn't need an internet-enabled litter tray that sends an amusing tweet every time my cat uses it.  But the world wasn't consulted, and got it anyway.  Follow Beemo the cat's poops at

Above: "You've put a Twitter-what on my litter tray now?!" - Beemo, the cat in the hat.

Random number generation on the ESP8266

Soooo random!
For an ESP8266 project that sends randomised tweets, I need to use a random number generator.  In such a deterministic system such as a digital computer however, true randomness is hard (impossible) to come by, so we must settle for a pseudo-random number - that is a number that is part of a seemingly random sequence and is generated algorithmically from the previous number(s) in the sequence.  It is good enough for our purposes.  The simplest implementation of a pseudo-random number generator (PRNG) is a Linear Congruential Generator, i.e.
Xn+1 = (aXn + c) mod m

Tweeting from the ESP8266

Other blogs have covered how to send data from the ESP8266 to a web server.  Sparkfun suggest a few services (here).  I've started playing around with ThinkSpeak.  The platform is great, and allows you to react to and visualise your data.  I have been looking for a way to post to Twitter from the ESP8266 for a project I've started working on and found that there wasn't a wealth of information.

Over The Air Firmware updates

Updating ESP8266 firmware over WiFi is the coolest thing *ever*.  I recently got it working on an ESP-01S board, and it was a breeze.  There is 1M of flash memory on my ESP-01S, and that might be important.  We need at least twice the sketch size space in order for the code to work.  There is something very satisfying about sending a program off over the home WiFi to an ESP8266 board stashed in another room!

The IR-egg: Voice-controlled ESP8266 I2S IR transmission!

I recently posted on ESP8266: Minimum I2S code required for use I2S to accurately control output to a GPIO pin.  I also previously used a UART hack to use serial output to control an IR led.  This approach was a bit hacky though, requiring a few transmissions of the UART IR signal to get proper reception.  I thought that using the I2S protocol would be a really simple way of implementing an accurate IR signal transmission, modulated at 38 kHz.

ESP8266: Minimum I2S code

Question: What's the most convoluted way to flash an LED on an ESP8266?
Answer: Using DMA (Direct Memory Access) and to output to a GPIO using the I2S protocol of course!

Outputting memory straight to an LED using I2S 

Adding LCD character display to Attiny85 project

LCD character displays are a pretty useful addition to a project for providing a simple test output.  However if we use them in 4-bit or 8-bit parallel-mode they can be pretty greedy on pins, not to mention the software overhead with custom libraries.  I've been programming Attiny85 chips recently, where pins are certainly very much at a premium.  I wanted to add an 4-line LCD character output to the project, and luckily there are version of LCD displays that use serial inputs (I2C/UART/SPI) to control the input, meaning that if we use UART we only have to use a single pin to control output - excellent!

Sending IR remote control signals

In my previous post, we looked at how to grab an 38 kHz modulated IR signal, analyse and process it.

Now we'll look at a couple of different ways to transmit a modulated IR signal - first with an Arduino Nano, and then with an ESP-01S board.

UPDATE: I have now used I2S to accurately transmit a modulated IR signal with the ESP8266 chip

Decoding 38 kHz IR signals with an Arduino Nano

So... I have ambitions to use an ESP8266 board to control an IR LED and switch things on and off according to Alex voice control.  I have discussed how to implement Alexa voice control on an ESP-01S board here.  In this blog post I'll discuss how to grab, analyse and process an IR signal, ready to be re-transmitted.  In this case, I am using an TSOP4838 IR receiver, which takes an IR signal modulated at 38 kHz and demodulates to the corresponding 1s and 0s.  What is signal demodulation? This explains signal modulation excellently (stolen from SB-projects):

Hey Alexa, switch on Project

Setting up Arduino IDE for ESP8266, ESP-01S and FauxMo. I've been interested in playing around with some ESP8266 modules for ages.  In case you have been living under a rock for the last few years, the ESP8266 has taken the hobby-world by storm as the way of getting WiFi easily into a project.  In fact you need to catch up, because the ESP32 has come hot on the ESP8266 heels.  Sometimes choice can be a curse, and it seems that way to me with the ESP8266 board - there are a veritable plethora of boards available. So where to start from?  I like to keep my form factor small and keeps things as simple as possible, so decided to opt for the ESP-01S board, which is albeit limited in terms of GPIO pins (only two), but is really the barebones of an ESP8266 board - small with no other voltage regulators, so we are in control.  In retrospect, I think I would now have gone for a Wemos Mini-D1 as you get so much more in terms USB hookup and voltage regulation, but actually I have a very pre…

Uploading custom HEX firmware to MicroPython board

In my last post we setup System Workbench and setup some flashing LED code, we compiled it and uploaded it to the Pyboard.  We had to generate a HEX file in System Workbench, generate a DFU file and then upload the DFU file to the board - phew!  It's actually fairly simple to automate the DFU conversion and upload process.  First, download the DFUSE-commandline utility, here:

Hacking a MicroPython board with custom firmware

I've had a MicroPython board sitting around my work bench for a while.  I think it's a brilliant way of interfacing hardware and using Python in a very straightforward way.  I've thinking about getting into into programming ARM chips for a little bit, and my MicroPython board came to mind.  After all, it's potentially a great development board if only we could hack the firmware that comes with it!

Controlling my quadcopter with a PS2 controller

I can now control my quadcopter with a PS2 controller - really cool!  The magic control box is powered either from the Arduino Nano inside and communicates with the quadcopter via radio, or a lipo battery, which is rechargeable over USB.  LEDs on the top feedback about the drone status and look really awesome.

UPDATE: I have now incorporated the control electronics, and a long distance radio module directly into the PS2 controller!

Height and vertical velocity Kalman filtering on MS5611 barometer: part 2

Adding accelerometer data to the Kalman filter In my last post I wrote about a Kalman filter to take the MS5611 barometer data and derive both the quadcopter height, and the vertical velocity.  It worked reasonably well but there was a compromise between noise and latency of the filter.  To get even better results, I have incorporated now the accelerometer.  We could produce a new Kalman filter using the height, velocity and acceleration in the state vector, but it turns out we can simply amend our previous filter and include the acceleration in the control vector to improve the predicted state.

Height and vertical velocity Kalman filtering on MS5611 barometer

Height and vertical velocity from a MS5611 barometer

Steps towards vertical flight control
This isn't a post explaining how Kalman filters work, I assume some working knowledge.  Instead I wanted to record the process of developing a Kalman filter to derive both the height and the vertical velocity of a quadcopter using a MS5611 barometer, as it may be useful to others.  After recording some flight data on my quadcopter EEPROM, I then spat the output to a PC for post-processing and model training.  In Python I used a Kalman class to generate the following Kalman filter (Wikipedia: Kalman filter) based on the barometric height data.

The Fast and the Furious World (of Trigonometric Functions on ATMega328)

Atan2 algorithms - speed vs accuracy Running trigonometric functions on an 8-bit AVR is inevitably going to be slow.  Floating point operations come with speed overheads and the most accurate algorithms usually come at a further timing price.  The arctangent function (atan/atan2) is ubiquitously useful, and especially so for Quadcopter's and robotics.  Briefly, an atan2function takes Y and X as arguments and works out which quadrant the tangent angle is in, and uses that to call a corresponding atanfunction.  I have been investigated the timings of various trigonometric functions on an ATMega328p chip running at 16 MHz, and in particular exploring 4 atan2 implementations and their trade-offs for accuracy.

Bluetooth programmer

From breadboard to perfboard I have been busy bending Bluetooth modules to my will.  The typical parameters I need to control are the module name, baud rate, and whether the module is a slave or master.  To program my quadcopter over Bluetooth, I have defined a Bluetooth module as a Master, which connects automatically to the quadcopter slave module when available.  The Master module connects to the PC via an RS232 USB converter - until now this been a mass of wires living on a breadboard, but I've now finally soldered a simple circuit together to allow me to program modules more easily.

Quadcopter code structure

I've been meaning to post this for a while but after soldering everything together and correcting the errors in the PCB (details blogged here: Resolving UART bus conflict and Power control), THE THING FLIES!  In fact it flies pretty well.
I thought I would record the code structure of the quadcopter. Below, psuedo-code for the quadcopter main loop, focusing on stabilisation:

A tale of two UART devices

Resolving UART bus conflicts and voltage levels I wanted to record two potential issues I found with using my custom Atmega328p board with onboard USB FT232 when an HC-05 bluetooth module in introduced, in the hope it may well be helpful to others.

Above:  A small piece of perfboard modifies the Bluetooth socket to include a 5V to 3.3V voltage divider on the Bluetooth HC-05 RX line (10 and 20kohm resistors).  The red wire provides a direct line from the Bluetooth HC-05 TX output and the Atmega328p RX input, thereby granting it preference when the HC-05 module is present.  It's not quite clear from the picture, but the RX input pin is bent up out of the socket and a 1k resistor connects the FT232 TX output (see below).

Customising Optiboot

I wanted to setup programming my custom Atmega328p board over Bluetooth.  Some have used the connection pin to control the Reset pin of the AVR.  However I want to use Bluetooth constantly, and only trigger a reset upon receiving a specific string sequence.  This requires tweaking the bootloader, using Optiboot as a basis:

(1) Change Baudrate to be mutually compatible with Bluebooth and AVR.
(2) Set an EEPROM byte in the main application, which could be read (and written) in the Optiboot routine.

Setting up Optiboot in the Atmel environment:
- Setup a project as usual in Atmel Studio
- Copy files over from github to the project folder: optiboot.c, pindefs.h, stk500.h and boot.h. EDIT: It seems there is an AtmelStudio folder already set up on github.

Setting baud rate to be compatible with AVR and Bluetooth module: 57600 bps

- Insert #define BAUD_RATE 57600 (as required) into optiboot.c.
- Compile and link project with following settings:
Compiler Misc: -g -Wall -Os -fno-split-wide-type…

Radio control

Accessing quadcopter memory over radio The usual method of controlling quadcopters is to transmit a train of PPM signals from a radio transmitter, measure the length of the individual pulses on a microcontroller, via a radio receiver .  As part of my DIY ethos, and as a learning exercise, I have made the above controller with an nrf24l01 moduled and an Arduino Nano.  In a nutshell, the Arduino Nano captures the 2D joystick positions, used for yaw, throttle, pitch and roll, and the button presses.  A data packet is then sent by radio to the Quadcopter.  In fact there is a more powerful implementation going on.  What is interesting is how the controller sends data to the Quadcopter - it actually manipulates the memory directly on the Quadcopter AVR - how does it do that?
Well, first of all the Quadcopter defines the memory addresses that the controller has access to by setting recording a pointer to the desired variable - note that the cast means that it doesn't matter whether the v…

Stuck in Atmega328 reset loop?

Kill switch killing... but stuck in reset loop

I wanted to add a kill switch to my quadcopter project so that in the case of crash/emergency, a button might be pushed on the radio controller, the quadcopter motors stopped and the flight controller chip is reset.  A good way of resetting an AVR is to invoke the watchdog timer and put the chip into an infinite loop - the watchdog times out and the chip is reset.  This is fairly straightforward when the kill/reset signal is received:

#include <avr/wdt.h>

void kill_drone(void)
USART_Pputstring(PSTR("Kill drone!\n"));


where the two watchdog handling functions are defined in avr/wdt.h.

The AVR is reset, but keeps on reseting, initialising and resetting in an infinite loop... Watchdog Groundhog day!

It turns out that the watchdog register settings are enabled, even after a reset!  To get around this, the watchdog must be turned off at the earliest opportunity of code execution by …

A few teething problems

A couple of teething problems in my board design. Lessons learnt and problems solved!
Design philosophy
Because you get 3 boards from OSHPark, and really I only needed one for this project, I wanted to 'future-proof' my board so that I could use the other two effectively as an Aruduino-type board for other future projects.  Therefore I have broken out every pin that I can and added convenient sockets for the following peripherals, which are likely to be useful in future projects (as well as in this one):

- Bluetooth module (HR-04)
- nRF240l radio
- GY-86 10-DOF module (MPU6050 + MC5883l + MS5611)
- Onboard EEPROM 4Mbits/500kbytes (a25l040-f AMIC)
- ISP: Programming chip and flashing with bootloader for Arduino GUI use.
- FT232RL for convenient serial USB communication.

It's always a bit of a pain to wire up the nRF2401 radios and with this board design I can very easily drop on into the correct socket, and also add a bluetooth module and not having to worry about wiring th…

PCBs have arrived!

Totally board A flurry of excitement this morning... The PCBs arrived from OSH Park!  I have to say they look gorgeous! I am in no way biased.

As a quick sanity check of the custom USB part, I dropped in a USB socket and it fitted like a dream.  I can't wait to break out the soldering iron! Fingers crossed there aren't any absolute clangers in the board design!

DMP 6-axis processing

Rock, Roll and Yaw... and Barometric pressure! A change to the Quadcopter design!  Previously I had MPU6050 on a GY-521 board (acc+gyro) and the magnetometer HMC5883L on a GY-271.  I decided I wanted to have a barometer on the flight board for height control.  A bit of research revealed the MS5611 was a pretty sensitive standard barometer.  There wasn't room to add a separate barometer module to the flight control board and in fact it was cheaper to replace the GY-521 and GY-271 board with a single board, GY-86, that contained the accelerometer, gyroscope, magnetometer and barometer!  On this single board we have the MPU6050 accelerometer and gyroscope, magnetometer HMC5883L and barometer MS5611.

Calculating quadcopter orientation So I'm using the GY-86 10 DOF board to calculate the orientation of the quadcopter.  We have the MPU6050 processing chip on board, and although we can read off the various sensors readings, a technology/software known as Digital Motion Processor™ (D…

Clone wars

Cloning Arduino core ... using TIMER0 in ArduinoSometimes you want to use TIMER0 in the Arduino IDE but default inclusion of Arduino libraries means that you can't have control over TIMER0 (it is used by millis() and delay() functions).  In a recent project, I wanted to use all the Atmega328 timers, and for various reasons, still use the Arduino IDE. The solution is simple.  Rather than hacking the core library, we'll clone the Arduino core library, define a board that uses the core library and then hack it to our specifications. 
(1)  Clone the Arduino core
Copy the Arduino core (C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores) as "arduinohacked" - the new core directory mustn't have an underscore in it!

(2) Define a new board to use the cloned core library
Open "C:\Program Files (x86)\Arduino\hardware\arduino\avr\boards.txt" and put the following text at the bottom.  This will define a new board in the Arduino IDE.  I'm using a Arduino…

Flashing SimonK Hex to ESCs

After some experiments applying PWM signals to an ESC controller for the Quadcopter, it rapidly became clear that the response was sluggish.  The "SimonK hex" firmware for ESC controllers appears to be the community's standard for rapidly responding ESCs.  I'm using the "MultiStar LBEC 2A 15A" ESCs from Turnigy.  After slipping off the ESC's cover slip with a razor the chip to flash becomes apparent - Atmel ATMega8A.
This video on Youtube was very useful:

Firstly the correct hex has to be chosen for these specific ESCs from a firmware archive.  These ESC appear to be discontinued on HobbyKing so I had to chose something that appeared very closely related - "kda.hex".

The flashing software, KKMulticopter tool, is a convenient way of doing things (though avrdude from the command line manually is of course possible).  From the KKMulticopter tool, choose

(1) Programmer = USBtiny simple USB programmer (or whatever yours is)
(2) Port = USB
(3) Contr…