Showing posts with label arduino. Show all posts
Showing posts with label arduino. Show all posts

Tuesday, 9 January 2024

Testing ICs


It seems like about two years ago, IC testers made quite a splash, judging by reviews of the Retro Chip Tester Pro (Adrian's Digital Basement) and the BackBit Chip Tester Pro (Noel's Retro Lab). Nice products though they are, they are a little out of my price range for this project.

TTL testing

Luckily for my wallet, there are several DIY projects out there which can be assembled from commonly-available parts in not too long. Nick Gammon (who's excellent interrupt and power-saving pages were inspiration for my Interrupted project) has a super-simple one which can be assembled on a breadboard in about an hour for the cost of 16 resistors, some dupont connectors and an Arduino Uno!

It ain't pretty but it works!

Nick credits an Instructable by one "JorBi" for inspiration for his work. That project is more elaborate, using an Arduino Nano and a display --- Nick's just uses the Arduino serial port, nothing wrong with that. JorBi's project also provided the data file containing the actual chip tests: JorBi stores this in an EPROM while Nick puts it in the on-chip flash.

Another tester of note is the Smart-IC-Tester: an Arduino shield with TFT display. This stores the test database on an SD card, in a format very similar to JorBi's, just adding a one-line description to each part.

So? Well out of 34 74-series TTL chips on the UK101, Nick's tester flagged 7 as bad, 15 as good, 4 were unknown and 8 not tested, due to having wires soldered to them. Not bad for a day's work!

The bad chips were as follows:

  • Two 74125s
  • Three 74123s (all from the same batch)
  • One 7403
  • One 7404 (in bad shape, one pin broke off in the socket)
Note, it could be that the 74123 and 74125 tests are incorrect. I have none which tested good.

Update: After playing with the IC-Tester for a while longer, I managed to add tests for the hitherto-unknown chips. The latest score is 9 bad, 22 good and 3 not tested. My fork of Nick's repo is here.

Update 2: I built this circuit on a piece of vero board and piggy-backed it onto my home-brew Uno!
An improvement anyway!

Memory testing

In addition to the discrete TTL chips, the UK101 also had 32 2114 static RAM for main memory and another 4 for the display. Nick's tester is unable to test this IC. Luckily somone named "skjerk" has written a sketch for the Arduino Uno which does exactly this.

The 2114 has a pretty bad rep when it comes to living into old age so I didn't hold out much hope for my set. Rather incredibly however they all came up good!

Untested so far

Chips which I haven't been able to test yet:
  • The 6502 and 6851
  • The Basic ROMs and Character Generator ROM
  • The monitor EPROM

Given an EPROM programmer, it should be possible to read the ROMs and compute their checksums. Stay tuned for that!

Of course the two commercial testers mentioned above can test all of these chips, out-of-the-box. But where's the fun in that?

Friday, 19 July 2019

KiCad Kata

Pretty (but dumb) LED Matrix
The picture shows the result of another workout with KiCad as executed by Seeed Studio and finished by me. This looks rather prettier than the original and was considerably easier to solder, not to mention more LEDs!

However a few defects are obvious too:

  • the LED spacing is wider than on stripboard, which can make patterns harder to discern
  • the interface split between anodes and cathodes is the same as the original, for no good reason
  • it now uses 16 gpios!
The first and second of these are my fault but the last could have been mitigated by adding an LED driver, such as the MAX7221 to the design:
MAX7221 DIP package
This particular chip requires only a couple of capacitors and a single resistor to function (see the circuit here). Also it is cheap, less than €1 from China and has a mature Arduino library. It could comfortably have fit in the space occupied by the resistors on the left of the picture. On the downside, it requires a 5v supply. (The Arduino playground has a lot of information about it.)

So, it was instructive to lay out an LED matrix and write a library for it, and I'd encourage you to try it for yourself. (However I also won't blame you for simply buying one on eBay, they seem to be cheaper than the cost of parts somehow!)

Wednesday, 22 May 2019

Soldering Kata

7x7 LED Matrix
When I were a lad, LEDs were a big deal, literally: £1 each. Now someone in China manages to make a living selling them for £1 for 100. With inspiration provided by an Instructable, a rainy afternoon and the need for some soldering practice I set to work.

The Instructable's, err, instructions were clear enough:

  • take a piece of protoboard (the sort with pads instead of strips)
  • solder each row of LEDs anode (longer) first to hold them in place
  • cut the cathodes to size and bend them through 90 to connect each row's together
  • test each row after soldering!
  • I then cut a piece of stripboard ("veroboard") into single strips and connected each column's anodes together in a sort of DIY dual-layer PCB arrangement, see below.

Rear view: anodes connected in columns

The last step was the only point where I deviated from the instructions. (I first attempted to fit a single piece of veroboard over all of the anodes but found this impossible: there was too much variation in their positions. Perhaps aligning each row of LEDs as it is soldered using veroboard might work?)

Next: mount vertically on breadboard, add a handful of resistors (470R) and connect to an Arduino!

I wrote an Arduino library for it, available on Github. It contains a couple of examples, the simplest can be seen running below. (The moving light makes keeping the camera focussed impossible, sorry.)




The disadvantages of this bit-banging approach to driving an LED matrix are fairly well-known:
  • it uses an excessive number of GPIOs (14 here)
  • the GPIOs themselves can't supply very much current, so the more LEDs lit, the dimmer they are
  • PWM-style dimming is not practical
  • timing is tricky: since it relies on persistence of vision to work, each refresh cycle must take no more than 40ms
For all of these reasons, using a dedicated driver chip is recommended. More when my MAX7221 (another £1) arrives on the ship from China in a couple of weeks!

Friday, 1 June 2018

It speaks!

Porting TTS to the ESP8266 processor has been on my to-do list for a while now, prompted by a comment on an issue I'd created to remind me. To do this, I realised that I'd have to reacquaint myself with PWM on the ATmega328p.

At its most basic, a PWM waveform is a square-wave with a varying duty-cycle. Both the frequency of the square-wave and the duty-cycle are determined on an AVR by the its built-in timers. PWM on Arduino pin 9 (hardware pin 15, OC1A), for example, is driven by Timer-1, a 16-bit timer.

Several PWM modes are available; TTS chooses the "phase and frequency corrected" option.


The diagram shows how this works to change the duty-cycle of the output pin:
  • the timer counts from BOTTOM to TOP and back down again
  • when it crosses the OCR1 threshold going up, it sets the OC1A output high,
  • when it crosses this threshold on the way down again, it sets the OC1A output low. 
Therefore by dynamically changing OCR1, we can change the "volume" on the output pin.

The final piece of the puzzle is how the frequency of the square wave is determined. Since the timer ticks on every clock cycle, it will take 2*TOP cycles to cycle from BOTTOM to TOP and back. The AVR has a clock frequency of 16MHz and PWM_TOP in the TTS library is defined as 1200/2. This gives a frequency of about 13kHz.

By default PWM on Arduino/ESP8266 has a range of 1023 and frequency of 1kHz. However it provides APIs to change these values. By setting them appropriately, the ESP8266 speaks!

Wemos Mini D1 with TTS amplifier and super-cheap speaker
The picture shows a Wemos Mini D1 connected to a modified version of the simple LM386 amplifier described at the TTS GitHub. (The modifications arose from a Hackaday article on the LM386 and are summarised in the GitHub's README.)

This amplifier sounds surprisingly good, considering its total cost was about €1. Hooking it up to a signal generator and oscilloscope produced the following pretty pictures.

At low volume, the output is a pretty faithful inversion of the input signal (a 1kHz sine wave):

Undistorted Signal
At high volume, the output is clipped by the supply rails (but still sounds pretty good to my untrained ear):
Clipped Signal
These pictures were produced using an Xprotolab Plain, an amazing combination of Oscilloscope, Signal Generator and Protocol Analyzer implemented on an AVR XMEGA. Buy one!
Xprotolab Plain

Tuesday, 29 May 2018

ArduTouch

One of the very few downsides of ordering PCBs from Seeed Studio's Fusion service is the shipping cost. While an individual PCB can cost less than $0.50 in multiples of 10, shipping to Western Europe can add another $20.

One way to amortize these costs is to add more PCBs to an order, either from my own "design pipeline" or by searching online for cool projects. One very cool project discovered in this way, was ArduTouch from Cornfield Electronics.

One the hardware side, this is basically a battery-powered Arduino Uno with an LM386 amplifier driving a 4-Ohm mini-speaker, or your HiFi via line-in. The keypad is very nicely integrated into the PCB, see my construction below.
ArduTouch with Batteries and Cheap Speaker

Software-wise its GitHub provides five (at the last count!) different synthesizer sketches and a library for constructing your own. As usual, sketches are compiled in the Arduino IDE and uploaded by FTDI cable. A nice touch is that they uploaded a BOM for Mouser, making ordering the parts a snip!

I have several PCBs left over from this order. If you want one, contact me through the Tip Jar on the right. However I would encourage you to support the guys who made this great project possible by buying one of their kits.

Wednesday, 8 November 2017

WifiWeatherGuy

My title will no doubt remind regular readers of 2013's Weather Guy, a weather app which completely maxed out an ATMega328. Well it's had a technology refresh replacing Ethernet with Wifi, XML with JSON and AVR with ESP. A surprising amount of display code survived the upgrade and can be found on GitHub (along with Eagle files and Gerbers).
WifiWeatherGuy

The hardware has also been simplified and now comprises merely:
The Wemos has been mounted on the underside of the board as can be seen above, because I soldered its pin headers on the wrong side; however this suggests to rotate the display by 180 degrees, making the build more compact.

The software is built on the following fine components:
Perhaps the most interesting aspect of this build is its configuration:
  • Stored in JSON format in the onboard flash filesystem
  • An index.html is served to a browser which loads the configuration file and initialises form input items (via Transparency)
  • Submission of the configuration form transmits the form values in JSON format to the server where it is stored back in the filesystem
  • The index page also supports firmware upgrade
This pattern, where the client assembles a UI for the user out of pieces obtained from the server is finally becoming prevalent in the wider web, but nowhere is it more useful than here where the client has maybe 200x the power of the serving microcontroller.

The Weather Underground API provides current conditions, a 5-day forecast and astronomical information. Two of these can be seen below.

Sunrise and Moon Phase



Current Weather Conditions

Friday, 6 October 2017

Autumn Update

Some recent updates on the Github:

  • Interrupted is now compatible with the Arduino (1.8.4 and above) and Energia (18 and above) IDEs. However you have to cut-and-paste the examples, for now.
  • Tasks is also compatible with the Arduino and Energia IDEs, and (maybe) PlatformIO. Ditto caveat about examples.
  • uC-Makefile has been updated for the latest Arduino and Energia IDEs. Unfortunately Energia has discontinued support for the Stellarpad, so this option is now busted.
  • invaders now compiles under Energia 17, the last release to support Stellarpads.
  • WeatherGuy has had an ESP8266 refresh as WifiWeatherGuy. Stay tuned for more information on that.

Friday, 17 February 2017

ESP12-e Emergency Development Board

Mini-breadboards or DIY ESP Development Board?

What do you do when you can't wait for cheap breadboards to arrive from China? Build a development board of course!

The picture shows the work of an afternoon, to make a dev-board for the ESP-12 "IO Adapter Plate Expansion" (ebaysoldered up last time, which inconveniently didn't fit any breadboards around the house.

The circuit is based on this one except that it's powered from the FTDI cable.

The following week the mini-breadboards arrived. Haven't used them yet!

Tuesday, 13 September 2016

Sonoff Wireless Switch

All for less than a fiver!
This little baby is an ESP8266-powered wireless mains relay from ITead. Although it comes with firmware installed which will suit most people, that requires an internet connection as it needs to talk to the Amazon cloud. Luckily for us, some people have done a lot of hard work to provide several flavours of open replacement. I went with the Arduino one and version 1.6.11 of the IDE.

Preparation

  1. Get the ESP8266 core for Arduino by following the instructions. I used the git version.
  2. Get the Arduino version of the Sonoff firmware and read the prerequisite section of its wiki.
  3. I also had to specify version 3.1 of MQTT in PubSubClient.h for correct interoperation with mosquitto on Raspian.
  4. Edit the sketch's user_config.h for your LAN.
  5. Under the IDE tools menu, set Flash size: "1M (64K SPIFFS)"
  6. Solder a pin header onto the board and connect an FTDI cable to the headers.
  7. Holding the Sonoff button down, plug the FTDI cable into the host and upload the firmware.

Setup

  • The Arduino serial monitor comes in handy for troubleshooting, set the baud rate to 115200.
  • The Arduino IDE allows specification of debug level, under the Tools menu, to get firmware debugging messages on the serial port. (Set this prior to upload, obviously.)
  • I had to enable 802.11b networking on my router, before the Sonoff would connect.
  • I installed mosquitto on a Raspberry Pi, and enabled rsyslog on it. I also installed mosquitto-clients on my laptop to keep an eye on the MQTT stream.
  • On power-up the Sonoff creates a temporary access point, allowing direct configuration of networking parameters, if you've made a mistake, at IP address http://192.168.4.1. (This configuration page is also available during normal operation, at whatever address you've configured for it.)
  • Once this firmware has been flashed, it can be upgraded OTA. (I installed nginx on my laptop and had it serve files from my ~/www directory to facilitate this.) 

Friday, 26 August 2016

A Tinier Tiny

Now even tinier!
The photo shows an upgrade to last year's tiny ATtiny development board: now even smaller and with fewer components but with better breadboard performance!

The schematic and board layout (for Eagle) along with gerbers can be found here. The pcb (from Seeed, 1€) was cut down to size with a Dremel.

Monday, 19 October 2015

The Dining Philosophers

It seems as if this blog has been veering towards software for a while now, and this post is about as far from hardware as it's ever been --- but hey, it has LEDs!

The problem of the Dining Philosophers was first described by Dijkstra in 1965 as an example of resource allocation in concurrent (operating) systems. Briefly, five philosophers sit around a table with a bowl of spaghetti in the middle, spending their time alternately thinking and eating. There are five forks at the table, one between each philosopher, and in order to eat, a philosopher requires those adjacent to him.

Two important properties to be addressed by a solution to the problem are: safety and liveness. The first means that the solution is free from deadlock while the second requires that every philosopher eventually gets to eat. There are many solutions but the one implemented here is that proposed by Dijkstra himself: freedom from deadlock is ensured by introducing an asymmetry: the lowest numbered philosopher reaches for his right-hand fork first while the others reach for the fork on their left.


In the solution, each Philosopher is implemented as a Task and each fork as a Semaphore.

Tasks are non-preemptive, light-weight threads of control, they run until they call a blocking function, for example waiting on a Semaphore or sleeping for some time.

There are two kinds of task, the implicit one which runs setup() and loop(), and explicit ones which inherit from class Task and must provide their own stacks, here, 50 words.

In the example, the main setup() initialises the Task system and starts the explicit tasks; it then implements the behaviour of Philosopher #4 in its loop().



In the video, red LEDs indicate a thinking philosopher and green ones a philosopher eating spaghetti.

As always, the Task library is available on Github for Arduino and Energia.

Monday, 11 May 2015

Interrupted

Interrupted is a new approach to developing low-power, low-latency Arduino sketches, which was motivated by two observations:
  • That the default mode of programming for Arduino is busy-waiting, and
  • The runtime support for power management provided by avr-libc is basic, being charitable to it.
Taken together, these two points mean that Arduino sketches aren't easily battery-powered, while the second results in a cargo-cult of code copying. Lastly, timed sleep in Arduino is provided by delay(), which itself is busy-waiting. This increases latency, the response time to external events such as button-presses.

And so to an example; the sketch below manages a very simple hardware configuration composed of an ATtiny85, an LED and a button: the button turns the LED on and a timer turns it off again. (You could imagine such a sketch driving a Useless Machine.)

In the main loop, the CPU sleeps in select(), waking up when an event occurs on one of the devices it manages. Three events are possible here:
  • An external interrupt which occurs when the button drives pin PB2 low,
  • A pin-change when the LED on PB0 is turned on or off,
  • When the watchdog timer fires, one second after the LED comes on.
The value returned from select() indicates which event has woken it up, allowing the concise construction of state-machines. If more than one event source is ready, the one added earlier in setup() is returned, implementing a crude form of priority.

The sleep-mode entered by select() is the maximum which makes sense for the devices currently active. In this little example, SLEEP_MODE_PWR_DOWN will be chosen because external and watchdog interrupts still work in this mode.

Much credit is due to Nick Gammon, for the great work contained in his Interrupts and Power Saving Techniques for Microprocessors forum-pages.

Very similar techniques were employed in the runtimes of the Conic and Regis environments developed at Imperial College (as well as the select system call, of course).

The library has been ported to ATtiny84, ATtiny85 and ATMega328 processors and is, as always, at GitHub. Its code footprint varies, depending on the functionality required by individual sketches, but the little example above comes in at 2.5kB.

Work remains to be done on this library including porting it to Energia (i.e., the msp430 family), porting existing sketches to use it and completing the repertoire of supported devices (e.g., SPI handling).

Wednesday, 11 March 2015

A tiny ATtiny

Very tiny!
 Recently I was surprised to discover that although I've made several projects using one, I'd never built a development board around the ATtiny85. With any project there are always design choices and a couple of important ones here were:
  • Is a breadboard optional or required? If optional, you get the familiar Arduino form-factor, which can either be connected to a breadboard using jumper cables or populated with pre-built shields.
  • Design a PCB or something more ad hoc? I wanted something quickly --- I wasn't going into competition with the Teensy or Trinket.
  • If not designing your own PCB, use stripboard or protoboard? While I've never been a huge fan of protoboard, because you're basically making your own strips by blobbing solder across pads, you can get quite a high component density with it.
So I opted for the path not taken, requiring a breadboard and using protoboard, and the result is pictured above. Notice that protoboard allows the FTDI connector to be attached at right-angles to the chip (try that with stripboard!) and also exposes more of the breadboard for jumper cables (the 10k resistor connecting reset and Vcc is underneath the chip).

With the ever-useful Tiny-Safe-Boot bootloader and a bespoke entry in my arduino-tiny's boards.txt file, it's as easy as regular Arduino, at a fraction the cost!

Wednesday, 12 November 2014

In the Beginning... was the Command Line

Say what you like about the Arduino IDE, it's certainly lowered the barriers to entry for kids messing about with microcontrollers. 

Oh wait, that's not what I wanted to say at all...

Sometimes the Arduino IDE is simply not enough. It just gets in your way. Your project has outgrown it.

These were a few of my more printable thoughts as I tried to force a quart of UK101 emulation into the pint-pot of that tiny editor window. At times I'd no idea which file I was even editing! I longed to bust out emacs (or even vi).

What I really wanted was make. In particular GNU make. Simply the best build-tool ever, no question.

Many people have written Makefiles to build Arduino sketches but none of them felt right to me. They were all too complicated, too monolithic. Or maybe it was just a case of not-invented-here. In any case, I present uC-Makefile, which supports Arduino-1.0 and Energia.

The best thing about it so far has been control over the compiler's optimisation levels. Compiled with optimisation -O3, that old microcomputer's emulated Basic interpreter ran almost 5x faster!

Bye-bye, Arduino IDE!

Monday, 9 June 2014

Starting with the LightBlue Bean

LightBlue Beans by PunchThrough

I have just received my LightBlue Beans. I ordered them before they were made, rather like Kickstarter (they used the dreadful word preorder). These boards are pretty neat, they have Bluetooth LE, motion sensors, temperature sensors and they are Arduino compatible with a few Arduino pins exposed and a small prototyping board to add extra functionality.

One nice thing about them is that they can be programmed wirelessly over BlueTooth, a great boon if you have one in your ceiling monitoring the mouse population up there!

Here they are in their boxes, along with a couple of developer's kits, resistors, leds, spare batteries etc.

My new goodies
Clearly labeled holes
It has a battery!

Installation on a Mac

Uh, oh! I do hope the devices themselves are better than the installation problems I've had. I have followed their instructions very carefully. Download Arduino 1.0.5 (not 1.5.x), move it into Applications. Download and install the Teensyduino software. Ok, that bit went well. Now all I have to do is download the LightBlue software installation package and I'm good to go. Great! No errors! Let's see, what's next?

"Choose the LightBlue Bean board" erm. What LightBlue Bean board? Nothing there! I deleted my Arduino and reinstalled many times, no go, the LightBlue Bean installation package simply did nothing, I just cannot see LightBlue Bean under the Arduino Board menu at all.

I checked out their web site and could see that quite a few people are complaining about similar problems. Someone from PunchThrough uploaded a zip file that was supposed help but that didn't work either, it was old software and was missing drivers.

So, I had a look inside the installation package. The first thing I saw, and didn't like, was that it expects to install software in /Application/Arduino.app (it still didn't work when I put my Arduino IDE there). It was also looking for specific versions of Teensyduino so maybe I didn't have the correct version but I had the only one I could download. Anyway, I didn't follow that through, I could see what had to be done (instructions lower down in this post) so I extracted the files from the package and installed them by hand.

Once I restarted the Arduino IDE everything finally worked.

Battery Woes

Well, almost everything!  I could only see a few of my eight boards and even then it was hard to connect to them.  I checked the battery on one and it was 2.7V, I checked a few others, well under 3V. Hmm, these batteries should be 3V, time to open the new ones that came with the boards and pop them in. This is a little worrying, these are new boards with (I imagine) new batteries in them but now they are run down; this does not bode well for future projects.

At least they are fairly easy to check using the BAT and GND holes

Feeding the Beans

Now the batteries are replaced I can see the beans. I have to connect to each one with a right mouse click but how do I know which is which?

Confusing display, it would be nice if they had the MAC address
All I have is signal strength and once they are connected, they are all called Bean.

So, which is which?
Once connected, the right-click gives me a few other options including Blink LED.


I tried that with one and a box lit up momentarily, it made me smile, nice design.

Here it is!
Ok, what else? Firmware upgrade. That is advised on the site so off we go, with new batteries I feel confident in upgrading the firmware. It takes a while, it is registering increments of 0.1%, I hate doing this, I always feel I am going to brick something.

Fingers Crossed!


Ok, firmware upgrade complete, time to get to work with them tomorrow!

Installing the LightBlue files by hand (Mac OS X only)

These instructions require you to use the terminal. If you haven't used it before take extra care typing in the commands, it is easy to make mistakes and you might move files to the wrong locations.

I am going to present the commands as a complete block that can be copied and pasted. You can do them one command at a time if you feel uncomfortable or want to see what is happening with each step. Notice that the last if statement covers several lines. Personally I prefer putting the commands in a file and executing them from there. However, I have tried copying and pasting them directly into a terminal and they worked fine.

Assuming that you have downloaded the installation package into Downloads and it is called LightBlueBean-MacLoader-1.0.0.pkg and your Arduino IDE is called Arduino in /Applications, you can finish your setup with the files using the following commands (adjust the first few lines as necessary, the .app after the Arduino name is hidden in the finder).

# Assume that the pkg file is in Downloads
cd ~/Downloads
IDE=/Applications/Arduino.app
PKG="./LightBlueBean-MacLoader-1.0.0.pkg"
TMP="/tmp/LBexpanded.$$"

# No need to change the following lines
echo "Checking directories and files"
if [ ! -e "$IDE" ]; then echo "$IDE not found"; exit 1; fi
if [ ! -e "$PKG" ]; then echo "$PKG not found"; exit 1; fi

# Open the package
echo "Unpacking the package"
pkgutil --expand "$PKG" "$TMP"

# Extract all the files
echo "Extracting files"
cd "$TMP/Arduino.pkg"
gunzip < Payload | cpio -i

# Now copy the files into place
echo "Copying files"
cd "./Applications/Arduino.app/Contents/Resources/"
tar cf - Java | (cd "$IDE/Contents/Resources" && tar xf -)

echo "Checking to see if the library was installed: "
LIB="/usr/local/lib/libBean OSX Library.a"
if [ -e "$LIB" ]; then 
    echo "yes, library ok"
else 
    echo "$LIB not found"
    cp "$TMP/Arduino.pkg/usr/local/lib/libBean OSX Library.a" /tmp
fi

# Tidy up
rm -r "$TMP"


If you get a message saying /usr/local/lib/libBean OSX Library.a not found then you will need to install this by hand because it requires administrator privileges. The following command will copy it over for you and it will ask for your password in order to be able to do it (note that this command is all on one line):

sudo cp "/tmp/libBean OSX Library.a" /usr/local/lib/.

Ok, now restart the Arduino IDE and you should see the correct board type and the LightBlue-Bean examples in the examples directory.


Sunday, 10 November 2013

ATtiny Development Board... now with added Bootloader!

Not long after writing about how ATtinys have no bootloader support, I came across Adafruit's Trinket, an obviously-bootloaded ATtiny85! However as this board's USB bootloader consumes over 2.5kB of flash, it could not coexist with wireless networking in the form of the RF24 library I'm interested in (which leaves only 2.5kB free after inclusion).

Nevertheless, googling "attiny bootloader" turns up several interesting links, notably TinySafeBoot. This is both tiny (550 bytes) and safe (providing password support). Its size means there's still almost 2kB left free in my desired configuration which I expect should be enough for some interesting wireless sensing applications --- stay tuned for more on that.

TSB, which is GPL-licensed, comes with a generic AVR bootloader blob which it tunes for your target hardware, rather than requiring messing with makefiles and building-from-source. In the case of our ATtiny development board, the desired target is an ATtiny84 with pins PB1 and PB0 connected to software-serial Rx/Tx:
% ./tsb tn84 b1b0  # generates tsb_tn84_b1b0_20131023.hex
The resulting bootloader can now be uploaded to the development board using an external programmer. Thereafter TSB talks to the bootloader to upload programs:
% ./tsb /dev/ttyUSB0 FW Blink.cpp.hex
(The bootloader auto-detects the bit-rate which the uploader is using; here I'm just taking the default of 9600B.)

The obvious next step is to integrate TSB into the Arduino IDE, building on the Jeelabs' work to obtain an environment indistinguishable from a normal ATmega-based Arduino; it would simply replace that part of platform.txt, specifying tsb, with appropriate flags, as the uploader.

Unfortunately, I think due to the way tsb handles its input stream, it doesn't take kindly to being called from a script; it's written in freebasic, maybe it's a feature of that platform. So, with the help of strace, I wrote a one-shot firmware uploader in C (gist here) callable from the Arduino IDE.

Now I can upload sketches from the Arduino IDE to my ATtiny development board without any additional hardware! The 'burn bootloader' functionality also works (with the aid of an external programmer) to burn fuses and install the custom firmware file generated above. My fork of Jeelabs' repository is available here, just clone it into your sketchbook/hardware folder.

The only hardware modification I made to my development board was the addition of a series 100nF ceramic capacitor between reset and DTR, to reset the ATtiny when the IDE wants to upload a sketch via FTDI.

Update

A diagram showing the pin names of the ATtiny84 in the Arduino IDE:
Here's a circuit diagram:
The schottky diode acts as a block to allow the board to be powered from an external battery. In this mode, the LED is disabled and no reverse current flows through the regulator. A schottky diode was used because of its low forward voltage drop (0.4v); an ordinary diode would drop the 3.3v supply to 2.6v. (Maybe that's why 3.6v regulators exist!)

Wednesday, 4 September 2013

ATtiny84 Development Board

JNµ-alike
 This little board was inspired by a series of articles at JeeLabs describing how to program a JNµ from Arduino-1.5. (I tweaked a couple of values in its boards.txt but otherwise it's as described there.)

Background: since the ATtiny has no bootloader support, it can't be programmed with an FTDI cable but requires an ISP (e.g., an Arduino running the ArduinoISP example) to upload sketches to it over SPI, via avrdude (as seen below). The latest version of the Arduino IDE now supports this from within the IDE itself.
Programming it.
However I chose to add an FTDI connector because the authors of the arduino-tiny core had cunningly added a transmit-only serial emulation (on pin PB0 on the ATtiny84). (While a SoftwareSerial library which can also receive data exists, it's not yet supported on IDE 1.5.2. However I added future support for it anyway by wiring the receive line to PB1.)

Board layout
But wait, there's more! This fine article points out an alternative to ArduinoISP, which can also act as a serial relay: TinyISP. It provides two ways of doing this: by relaying SoftwareSerial and a (more robust) alternative called Knock-Bang. Each of these methods uses the SPI lines so no extra connector is required. This allows programming an ATtiny in almost the same way as an Arduino --- except you need an Arduino to act as a serial port for it!

Monday, 29 July 2013

Weather Guy


The picture shows a little Weather Station based around an ATmega328. What makes this one interesting is that it gets its weather data from Yahoo! Weather rather than a bunch of sensors. What's interesting about that (to paraphrase Dr Johnson) is that it can be done at all on an 8-bit microcontroller.

The hardware components of this system are not worth drawing a circuit diagram for:
  • Basic ATmega328 with 16MHz crystal and 22pF balancing capacitors (you know the score)
  • An ENC28J60 ethernet module (e.g., from eBay)
  • An ST7735 TFT display with SD-card (e.g., from Adafruit)
  • Stripboard, connectors, etc.
  • Err, that's it!
The software is where the real action lies. This baby (a 600+ line sketch!) crams in the following libraries:
The restrictions of the AVR chip steered the implementation in interesting directions:
  • All string constants stored in flash, using either F() or PSTR().
  • Using the TFT screen for debugging rather than Serial (saves 128 bytes RAM).
  • Graphics downloaded from Yahoo! in advance, converted into bitmaps and stored on a cheap SD card.
  • Buffer-sharing between the XML parser and reading from the SD-card. (In particular, most SD-card libraries require their own 512-byte buffer for output, see this earlier post.)
  • Reclamation of some flash on the AVR using Optiboot.
  • Displaying wind-direction without using transcendental functions (i.e., sin, cos) used the nice trick of successive rotations. (This is apparently due to the legendary Marvin Minsky, in hakmem 149.)
There is almost no free space left on the chip: just under 200 bytes of RAM (which I guess is needed for the stack) and 512 bytes of flash!

Here's another picture showing forecast data:


Saturday, 27 July 2013

Optiboot

The heart of a cheap Arduino clone is an ATmega328 chip. Often sellers of this chip will assume you're going to use it for an Arduino and burn a bootloader onto it before shipping it to you. But what if you get one without a bootloader? What if you want to use a different bootloader, for example Optiboot?

Optiboot is great: it fits inside a single 512-byte sector of flash, freeing up a (sometimes) precious 1.5kB for your programs. It allows downloading at 115200bps, also handy for large sketches. In conjunction with a 100nF capacitor between RTS and RESET, it turns your homebrew clone into an Arduino Uno (from the point-of-view of the IDE anyway).

So (adjusting your avrdude settings accordingly), firstly read the fuse bits:

$ avrdude -q -q -P /dev/ttyUSB0 -b 19200 -c avrisp -p m328p -U hfuse:r:-:h -U efuse:r:-:h -U lfuse:r:-:h
0xda
0x5
0xff

Check this online calculator for the meaning of these bits. In this case, the value of the high bits (0xda) means that 2k is reserved for the bootloader section, we're only going to need 512 bytes:

$ avrdude -q -P /dev/ttyUSB0 -b 19200 -c avrisp -p m328p -U hfuse:w:0xde:m 

Next program the optiboot bootloader (the version shipped with the Arduino IDE is in hardware/arduino/bootloaders/optiboot):

$ avrdude -q -P /dev/ttyUSB0 -b 19200 -c avrisp -p m328p -U flash:w:optiboot_atmega328.hex -U lock:w:0x0f:m

Now when you power up your Arduino clone, you should see a triple-flash on the LED on pin 13 indicating that Optiboot is ready to receive your sketches.

Friday, 19 April 2013

Cheap TFT Displays

The photo shows a (very) cheap 2.4" TFT LCD/SD/Touchscreen combo purchased (as always) from China, via eBay and PayPal. (Love those blemishes!) Here is what the manufacturer thinks it can do:

This device is a parallel TFT display, unlike the SPI one discussed previously. Its bus can be either 8 or 16 bits wide. Even in 8-bit mode it uses up a whopping 12 I/O pins.

It is well supported (on Arduino anyway) by Henning Karlsen's excellent UTFT library. In the picture it is running the UTFT_Demo_320x240 (configured as S6D1121_8, should you care). However you should follow his advice and edit memorysaver.h in order to comment out all of the other devices supported by this excellent library. Even so, the sketch comes in at over 25kB (and you have to partially disconnect it before upload if you have a USB-Serial Arduino-alike because two of those I/Os belong to the hardware serial port).

So this device pretty much maxes out an ATMega328. About the only application I can imagine for this combination is some sort of digital picture frame with touch-swiping. Even then it would be a tight squeeze to get the sketch and the further SD and UTouch libraries in under 32kB --- and that's without reckoning on the I/Os needed to drive the two extra devices.