loading

Why did I do it?

I wanted to make a very simple and low power-consumption AVR project with small footprint, for which a full fledged Arduino board would have been an overkill, feature-, power-consumption- and cost-wise.

Disclaimer

I cannot be held accountable for any damages caused by following this tutorial. Follow it at your own risk!

Prerequisites

    • Mac
    • Arduino IDE 1.6.11
    • Arduino Yún (I’ve got a Yún, but it should work for other Arduino boards just fine)
    • Jumper wires
    • A ~$1 ATtiny85-PU20 DIP-8 IC (This will control my tiny project instead of the the Arduino)
    • Breadboard or strip board + 2x 4 pin header sockets + DIP-8 IC socket
    • Basic command line knowledge
    • /usr/local/bin is on your PATH and you have write access to it

    Step 1: Set Up the Command Line Tools

    We're going to use the AVR compiler toolset directly from the Arduino App bundle.

    Note: I assume that you've installed the Arduino App to /Applications.

    Fire up the Terminal App and change the working directory to /usr/local/bin.

    cd /usr/local/bin

    Copy-paste and execute this single line command in the Terminal window. It will symlink all the AVR compiler toolset commands and you will be able to use them from the command line from now on.

    for i in `echo /Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avr-*`; do ln -s $i .; done

    Note: I had to create a wrapper to be able to use the avrdude command from the command line. (That's because I've chosen not to install additional software but to use the Arduino App's tools)

    Save the following shell script under the name "avrdude" in the same directory (/usr/local/bin).

    Note: This shell script is for the Arduino IDE 1.6.11 only. For other versions you might need a slightly modified version, or not at all.

    #!/bin/bash
    prefix="/Applications/Arduino.app/Contents/Java/hardware/tools/avr"
    export DYLD_LIBRARY_PATH="${prefix}/lib"
    [ -x "${prefix}/bin/avrdude_bin" ] || chmod +x "${prefix}/bin/avrdude_bin"
    exec -a "$0" "${prefix}/bin/avrdude_bin" "-C${prefix}/etc/avrdude.conf" "$@"

    Make it executable by executing the command below in the Terminal.

    chmod +x avrdude

    Now you're all set! You can compile, build and program your AVR from the command line using the tools from the Arduino App bundle.

    What's still missing is a way to automate this process, and it will be done by a Makefile, what I'm going to show you in the next step.

    Step 2: Create a Command Line Project

    Create a new folder for your project. Save the following content under the name "Makefile" in that folder.

    #
    # Makefile for the AVR Command Line Projects # # Copyright (c) 2016 Imre Horvath # Released under the MIT license #

    PART = attiny85 F_CPU = 1000000UL PROGRAMMER = arduino PORT = /dev/cu.usbmodem1411 BAUDRATE = 19200 BITCLOCK = 20 LFUSE = 0x62 HFUSE = 0xDC EFUSE = 0xFF

    OBJECTS = prog.o OPTIMIZE = -Os

    PRG = prog CC = avr-gcc CXX = avr-g++ CFLAGS = -I. -g -Wall $(OPTIMIZE) -mmcu=$(PART) -DF_CPU=$(F_CPU) CXXFLAGS = $(CFLAGS) LDFLAGS = -Wl,-Map,$(PRG).map OBJCOPY = avr-objcopy OBJDUMP = avr-objdump AVRDUDE = avrdude -v -c $(PROGRAMMER) -p $(PART) -P $(PORT) -b $(BAUDRATE) -B $(BITCLOCK)

    .PHONY: all clean flash read setfuses avrdude

    all: $(PRG).hex $(PRG).lst

    $(PRG).elf: $(OBJECTS) $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^

    %.hex: %.elf $(OBJCOPY) -j .text -j .data -O ihex $< $@

    %.lst: %.elf $(OBJDUMP) -h -S $< > $@

    clean: rm -f $(PRG).hex $(PRG).elf $(PRG).map $(PRG).lst $(OBJECTS)

    flash: $(PRG).hex $(AVRDUDE) -U flash:w:$(PRG).hex:i

    read: $(AVRDUDE) -U flash:r:read.hex:i

    setfuses: $(AVRDUDE) -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:$(EFUSE):m

    avrdude: $(AVRDUDE)

    Note: Adding additional sources to the build requires you to list the object file names in the Makefile (not the source file names!) like this:

    OBJECTS = prog.o wdt_delay.o pwm.o util.o dht/dht.o

    Note: With this, you can control which source gets included into the build and which not. It's useful if you have additional sources in the folder for trying things out...

    IMPORTANT! The actual clock speed of the microcontroller must match the F_CPU define provided to the compiler. That's 1 MHz for the ATtiny85 (factory default).

    Now you can build this simple blink example project by issuing: make

    And you can clean up by issuing: make clean

    Further options will be covered in a later step.

    Step 3: Turn the Arduino Into an AVRISP

    I'm using the most recent version of the Arduino IDE, which is the 1.6.11 at the time of writing this tutorial.

    Note: This version is shipped with a sketch called ArduinoISP which is know to work with all kinds of Arduino boards. However older versions of the IDE may contain some older version of this sketch which might not work with a Leonardo-based Arduino board like the Yún.

    Open the Arduino App.

    Go to File > Examples > 11.ArduinoISP > ArduinoISP

    Hook up your Arduino via USB to your computer.

    Make sure you've selected the right type for your board in the Tools menu. In my case it's:

    Tools > Board: "Arduino Yún" > Arduino Yún

    Also check the Port settings to match your board right under it.

    For me it's: /dev/cu.usbmodem1411

    If all is fine, then upload the sketch to the board by clicking upload.

    That's all! You've turned your Arduino into an AVRISP.

    Step 4: Hook Up the Programmer HW

    I've created a small board for the ATtiny85 as shown above, using a piece of stripboard, an IC socket and two header sockets. It can be used for a project or for programming the AVR too.

    After cutting the stripes (red arrow), check with a multimeter in continuity test mode that there are no shorts.

    Color-coding the connections and using a visual reference helps a lot! Hence the hand-drawn figure and the jumper cables with different colors.

    Do not connect the Arduino to your computer yet. First make the connections, make sure that they are correct. Finally if all is OK, then connect the Arduino to your computer.

    You're good to go to the last step, where you actually program the AVR.

    Step 5: Program Your Code to the ATtiny85 Using the Arduino

    So the Arduino is connected to the ATtiny85 and the whole is connected to your computer via USB.

    Before writing to the AVR, it's always as good idea to "ping" the setup first.

    A totally safe thing to do is:

    make avrdude

    This will check both the programmer and the part and will dump some useful information, like fuse settings, flash, eeprom, etc.

    If this went OK, then you might want to consider saving the current content of the flash before programming it with your program. This can be extremely useful if you don't have the source for the program or you don't want to recompile it.

    You can save the current contents of the flash to an ihex file, by issuing:

    make read

    This will dump the flash into a file called "read.hex" in the current directory.

    Finally, if you're ready to flash your program, execute the the following command in the Terminal.

    make flash

    Note: Pay attention to the output of the avrdude command. If all went OK, then you should see something similar as seen on the attached screenshots.

    Note: The Makefile is written so it can program fuses too, but be warned, messing with fuses can lead problems!

    References:

    ATtiny85 Data Sheet: http://www.atmel.com/images/atmel-2586-avr-8-bit-...

    Fuse calculator: http://www.engbedded.com/fusecalc/

    <p>Hey,</p><p>By Using Arduino -- </p><p>I want at 10 PM all my home lights should OFF and at 6 AM same should be ON.</p><p>I have set up the Arduino Uno board with DS3231 RTC but only unable to tell Arduino to read that particular time (from 10PM to 6AM) from RTC and to execute the task.</p><p>Kindly suggest all the possible method.</p><p>Thanks.</p>
    <p>Nice. That could save a lot of space on all my microcontroller projects.</p>
    <p>Thanks! It was really fun to try out programming the AVR &quot;directly&quot; using the already available tools for Arduino development and with such a minimal investment. :)</p>

    About This Instructable

    407views

    6favorites

    More by imrehorvath:Arduino Yún With DHT22 Shrinking My Tiny Arduino Project 
    Add instructable to: