Putting the ATTiny13A to sleep in power down mode, and waking it up again with the watchdog timer

I had a hell of a time trying to get this to work. Most of the code out there is for the ATTiny85 with the Arduino bootloader, which I’m not using. A lot of the code that I found was old and/or wrong. The AVR programming documentation is somewhat terse and hard to deal with.. not fun at all. All I wanted to do was to find a way to put the ATTiny13A to sleep in power save mode, then wake it up again with the watchdog timer. This just for blinking an LED slowly (slower than in this code) for use as decorative elements in model making. The models are small so the LEDs will be powered by small cells such as LR44’s or R2032’s, so I need to be power efficient. At time of writing, I still haven’t got Atmel Studio (formerly AVR Studio) to run AVRDude properly, but it works fine on the command line, so that’s a fight for another day. On that note – if you want to develop code for the ATTiny13A without the Arduino IDE, you need to install WINAVR even if you are using Atmel Studio. Also, if you’re on Windows, and you’re using a USBASP to program the Tiny, google for an app called “Zadig”. It can install the correct driver for you – you need to choose libusbK rather than the default option.

Using an Arduino Leonardo to program an ATTiny85

The ATTiny85 is a pretty neat little microcontroller with some sophisticated features, despite its small size, low cost, and limited I/O. I bought a bunch of them a while back to mess with, and recently got around to getting them to work. I have a few applications that could use these.

One thing that I don’t have, is a dedicated programmer for the AVR chips. If you’re using a naked microcontroller chip that’s not on a board, you’ll need to program it somehow! I read somewhere that it’s easy to use an Arduino to program these chips, so I never bothered buying a programmer. In theory, it is easy. In practice, it’s a bit of a pain, so I thought I’d write up my experience.

I was using a Leonardo because I don’t have a Uno, which is the board of choice when programming naked AVRs. The Arduino IDE comes with a program that you can use to program these chips, but to run on a Leonardo, it needs a small modification, which I’ll come to later.

After following various guides and failing, I found this instructable: http://www.instructables.com/id/Arduino-Leonardo-as-Isp/

It’s pretty good but some parts of it are quite out date. Here are the up to date instructions!

FIRST – install ATTiny support in the Arduino IDE. This was where I first got really messed up, as there are a lot of obsolete instructions out there. It’s actually very easy, you don’t need to edit any config files, all you need to do is open the Arduino IDE and go to File->Preferences, find Additional Board Manager URLS and paste this into the box: http://drazzy.com/package_drazzy.com_index.json

boradman

Close this window and go to Tools -> Boards -> Boards Manager. Scroll down to ATTinyCore, click it, and click ïnstall”. Apparently there’s a bug in verion 1.6.6 of the IDE and you need to close the board manager and re-open it before you can do this.

corepic

You can find more details about ATTinyCore here: https://github.com/SpenceKonde/ATTinyCore

Connect your Leonardo to your computer via USB. Go to File->Examples and load up the sketch called ArduinoISP. This sketch needs a couple of changes. Scroll down and find this bit:

sketchchange

Change RESET from SS to 10. Change LED_HB from 9 to 13. What this means is that we’ll be resetting the target hardware with pin D10, and instead of having to fit an LED to pin 9, we can watch the on-board D13 LED throb to show that the board is alive.

Save the sketch under a different name for future use. Upload it to the board – the D13 LED on the board should start to pulse.

Now it’s time to wire up the ATTiny. Locate the ICSP header on the Leonardo (I stole this image from Instructables.com, sorry)

FR9T9HRH3XML7BK.MEDIUM

The pinout on the ATTiny85 is thus (image purloined from highlowtech.com)

ATtiny45-85

MISO, MOSI, and SCK should be connected from the Leonardo header to the chip. You don’t connect MOSI to MISO or anything, as you might suspect: it’s a straight connection.

Connect VCC and GND. I didn’t use the power connectors on the SPI header, I took them from the 5V and GND connectors labelled on the Leonardo. This was purely superstition on my part.

Connect Reset on the ATTiny to D10 on the Leonardo.

I also connected the anode of a LED from pin 5 on the ATTiny, and connected the cathode to ground via a 220 ohm resistor. This was for testing, we’re going to upload a modified BLINK sketch to the ATTiny.

When you are all wired up, load the Blink sketch from the examples. change the pin from 13 to 0. There is no D13 on the ATTiny 🙂 the LED on pin 5 is connected to the ATTiny’s D0.

Your code should look a bit like this:

blink

Now use Tools->Board to switch your board in the Arduino IDE to “ATTinyX5 Series”.  Then go to the same menu and set your programmer to “Arduino/Leo As ASP (ATtiny)”. You’ll need to burn the bootloader before you can program the chip – you only need to do this once. Choose Tools->Burn Bootloader. It will take a little while, and if you haven’t wired everything up right, it will poop. Hopefully it’s all good.

When that comes back, you can just upload the blink sketch like you normally would. The LED connected to the ATTiny should start flashing.

When I tried to set this up, I had a lot of problems. My main trouble was old, obsolete how-to’s, particularly information on how to install the ATTiny support into the Arduino IDE. Also I forgot to change the pin on the Blink sketch, and I thought I had bricked my ATTiny, I just couldn’t work it out. Here is what my breadboard looked like at the end:

tinydone

You can just about see the ATTiny.

One of the many things that I got into a pickle about when it didn’t work (because I didn’t change Blink’s output to D0) was that I suspected that the ATTiny wouldn’t work unless I disconnected all the wires. Nope – you don’t have to. You can leave those wires where they are.

I hope this helps someone!

 

Multirotor Escalation

My lovely wife gave me a Cheerson CX-20 quadcopter for Xmas, and I’ve been tinkering with it, and adding stuff to it, ever since. The only problem now is, that I’ve run out of room. I had to mount the FPV transmitter to a landing skid, which is a really bad place for it.

20150606_175439

The FPV transmitter sends video back from a camera mounted at the front of the quad. The receiver sends the video to a pair of video goggles, and I fly it as if I was on board. FPV = “First Person View”.

When I wanted to add the OSD board (On Screen Display) so that I could have a HUD superimposed on the video, driven by the telemetry output from the flight controller, I found that the only place to put it would be to stack it on top of the FPV transmitter. So, a bad idea suddenly became a lot worse.

I didn’t really want to hack up the airframe of the quad, as it’s an “exoskeleton” and its strength comes from its structural integrity. Then I had an idea: why don’t I get or make a new quad frame, and just move all the gubbins from the Cheerson onto that? I can design it with all the room for crap that I want.

This design looked good, as it’s cheap and easy to build, and has a lot of space for stuff: Formica quad with FPV

I mentioned this to my wife, and she said, “Why not keep the Cheerson as it is, and build a whole new one?”

This was a great idea. Quadcopters don’t have to be expensive to make, and this would be great fun.

Then she said, “I think you should build an octocopter!”

This seemed like a bit of a crazy idea, so I suggested a hexacopter as a compromise. “No! You must build an octocopter!”

Well, my wife is always right, so the octocopter project began. Despite this massive escalation from the initial project, things were going to get even more crazy. Little did I know the rabbit hole of madness that I would shortly descend into.