Blink’n a light: Intro to the ATMega238(P)


In the Internet of Things movement, people across the globe are connecting their stuff to the internet. All the data you can imagine is being transmitted from our things — our TVs, our pets, our houseplants — to the internet.

If you’re going to be a part of that movement, or want to dabble in creative prototyping on a budget, it’s important to get to know our little friend:

The ATMega.


The real benefit: it’s only $4 US. Photo by Will Carlson.

The real benefit of using this microcontroller is that it’s only $4 US, whereas other micro-controllers are 10X that. It can also be easily programmed in the universal programming language, C++. The ATMega is also equipped with a decent amount of memory for any project.

Applications for the ATMega continue to grow across the global tech sphere. Today, it’s mostly used in simple machines to receive, interpret, and output information (useful information, and totally useless information).

You may have seen the ATMega used in small machines like RC cars and robots. It can make them autonomous and allow these devices to get from point A to point B on their own.

Thus, for its size and its cost, this is a powerful little device. Jaycon Systems is here to equip you with the know-how to put it to use!

Before we begin, you will need a 5v power supply.

For our demos, we’ll use a smaller version of this 5v power converter by co-founder and friend, Jay. I will also provide a picture and schematic for the miniature power converter.

All of the hardware required for this project can be found at our online store. Stay tuned: you’ll soon be able to by this as a kit. We’ve got your covered ;)


Hardware needed:

2x 0.1 uF Ceramic Capacitor ($0.25 for 5)

1x 0.1 uF Capacitor ($0.25)

1x 16MHz Quartz Crystal Clock ($)

1x AVR ICSP Programming Adaptor ($1.95)

1x ATMega328P ($3.99)

1x LED 5mm (any color) ($0.25)

2x 330 Ohm Resistors ($0.25 for 5)

1x USBTiny AVR Programmer ($14.95)

Total: ($21.64)

Software needed:

WinAVR [specifically, Programmer’s Notepad]

The ATMega328 is a microcontroller with 23 IO pins, two 8-bit internal clocks, and 32kB of flash memory.

To begin: notice the notch that indicates the direction of the chip — and the dot that indicates Pin 1. All pins that follow suit are in regular numerical order. If you are having trouble with the layout, you can view the product document (details on page 2).


First, add a wire from Power to Pin 7 (Power) and a wire from Pin 8 to Ground. Then, add the 18pF Capacitor to Pin 7 and Ground.


Add a LED to the first available row next to Pin 1 that is not already being used by the ATMega. Add a 330 Ohm Resistor from Pin 1 to Power. Pin 1 is the reset pin.

Then, add another 330 Ohm Resistor from Pin 4 to the row that the LED is connected to.

You have now powered the LED.

Note: the power applied from the Resistor to Pin 1 controls what “LOW” should be so the ATMega doesn’t constantly reset itself. The LED also needs the resistor so the ATMega doesn’t kill the LED.


The ATMega’s clock is slow and unreliable because it’s not constant. So, let’s add a crystal clock that will speed it up and make the ATMega more reliable. Add the Quartz crystal clock to Pins 9 and 10. Then, add a Capacitor from Pin 9 to Ground, and from Pin 10 to Ground. (NOTE: It’s important to not power the ATMega with the clock already installed unless it has been flashed, first).


Great job, mate. You now have a working circuit. All need now is the programmer…

Solder the programming adapter together and place it on the board. Make sure that you place parts in the correct direction, and be careful handling hot tools.


Great, you’re almost ready. The next step is to wire the programming adapter. You can take any path to wire it in, as long as you use the correct pins.

Optional: If you are going to power your ATMega with the programming adapter, wire from the “GND” and “+5V” pins on the adapter, across to the power rails.

Here is the Pin map for wiring the rest of the adapter: 17 to MOSI; RST to 1; 19 to SCK; and, 18 to MISO.

The USBTiny has a small switch on it labeled “NO POWER” and “POWER TARGET”. To power the ATMega from the USBTiny, set it to “POWER TARGET”. Not interested? Simply set it to “NO POWER” to avoid unintentional destruction.


Now that we have the board completely built, let’s program it so we can make the LED blink. Once it blinks, you know that you have properly built the board.

Ready for next steps? First, we need to make the ATMega use the clock that was just installed.

Download and install WinAVR, from this page.

WinAVR is a full suite, with a compiler, programmer, debugger, and more! Use these for the USBTiny. It will include Programmer’s Notepad, which is what we are going to use to program the ATMega, and AVRDude and will burn fuses and act as a backup for programing the ATMega.

Find “Run” on your computer, type in “cmd” and click “OK”.


Type “avrdude -c usbtiny -p m328p -B 25 -U lfuse:w:0xFF:m -U hfuse:w:0xDE:m -U efuse:w:0x05:m”

-C identifies the programmer “usbtiny”

-P identifies the chip being programed “m328p” (short for ATMega328p)

-B sets the clock rate. we are setting it to 25 because the current clock is much slower than the clock on the programer.

-U Is a memory operation, lfuse selects the low fuse, w tells the program to write it, and the hex code (0x##) is the fuse value. This is repeated for the high fuse and extended fuse.

Understanding Hex codes:

hex codes may look intimidating, but they are really just counting, using an extended list from 0–15, starting with 0–9, and then continuing with a-f (filling the 10–15 places). The 0x in front of each code is how the software knows that it is reading a new value, because it never uses “0x”.


What happens if you want to count a number bigger than 15? Hex, like normal math, just counts up, rounding back to 1 followed by a 0; so 10 means 16, 11 means 17, and so on, like normal counting, but with more digits.


So 0xd3 just says 211, and if you want to say 75, just write 0x4b.

This sets the necessary fuses the chip needs for the clock without killing itself. Once it is done, we are going to use Programmer’s Notepad to allow you to do a lot more with the chip than what you can do using Arduino.

Once the fuses has been burned, download and install WinAVR, from this page. WinAVR is a full suite, with a compiler, programmer, debugger, and more! We will use these for the USBTiny. It will include Programmer’s Notepad, which is what we are going to use to program the ATMega. Now, download “” This file contains the basics for making your LED blink, and the “makefile” that tells the programmer what your microcontroller is, it’s settings, and how to use it. Unzip “” and launch Programmer’s Notepad. Open “blink.c” and “makefile”. While on the “blink.c” tab, go to “tools > make clean” and run it. This will clean any unwanted data on the ATMega and compiler. Then click “tools > make all” and finally “tools > program”.


The command “make all” compiles and checks all the software from the two tabs, while “program” programs the ATMega with the freshly compiled software. This is nice because it allows you to compile the software once, and program multiple chips, one after the other, without going through the whole process again! Trust me, you will be thankful once you start making bigger programs.

Explaining the Code

You may notice a semicolon (;) after every line of code. The semicolon tells the program that this is the end of the command. The reason you need this is because when the program reads it, it doesn’t see multiple lines from when you hit “Enter” or “Space”. Instead, it sees it all as one continuous, massive line of code, so it needs the semicolon to know when to start interpreting a different command.

“while(1)” is a simple way of making a continuous loop. The “while” command repeats everything inside the “{ }” immediately after it, as long as the statement in the “( )” is true. Because it has been set to “1”, with no real variables or math, the statement will always be true.

“_delay_ms(500);” You can probably guess what this does: it adds a delay to the program, in milliseconds. One millisecond is 0.001 seconds, so saying “delay_ms(500)” means wait for half a second (0.5s).

The phrase “PORTD = 0b000001000;” translates into “Turn on Port D, number 4”, and all of those zeros are actually the different port numbers. So this:” PORTD = 0b000001000;” is really this:” PORTD = 0b87654321;”, placing the 1 on the ports you want to turn on, and 0 on the ports you want off.

You may need something smaller, or need something to handle more data; otherwise, this baby can do anything that your average project would require. It’s rare that anything will overpower the ATMega. WEarable electronics, turn to the AT Tiny.

This article was published by the Jaycon team. Learn more about how we can take your product design and hardware idea to the next level here.