Introduction: Getting Started With Atmel AVR and BASCOM

About: I like to create YouTube videos and have a channel. I also like to work with CNC, electronics, and I build robots. It's been awhile, but another hobby is high powered rockets.

I have seen plenty of Instructables showing how to work with microprocessors, but they all assume that you have worked with them before and know what you are doing. I have not seen an Instructable that takes you from nothing and builds on each step.

What we will do here is to start with a bare breadboard and build each connection and each component until we have everything we need to program a microcontroller to do something. In this Instructable we will blink some LEDs in sequence... then if you build this circuit... your first project can be to change the code slightly to make it into a traffic light.

I picked an older Atmel chip, the Tiny-26 to get started. It is a smaller microprocessor, very inexpensive, and easy to understand. Once you understand what we are doing here, you may want to try a more powerful chip like the Mega-328P which has more pins and more memory.

Note: The Tiny-261, Tiny-461, and Tiny-861 are pin compatible newer versions of the Tiny-26. They have 2K, 4K, and 8K of memory. If used, simply change the header by selecting the appropriate chip and recompile the program to use the newer version. The new chips have more functions that can be assigned to each pin. See the datasheets for more details.
Tiny 261, 461, 861 Datasheet (PDF)
Tiny 26Datasheet (PDF)

Below is an image with the pins for the chip we will be using... we will be connecting the power and ground... in this case 5v. So where do we get 5 volts? We will build a power supply from a 9v battery.

Let's get started!

Video posted in a larger size at:

Step 1: Breadboards and Building Circuits.

Below you can see a breadboard... they come in many sizes depending on how complex the circuit you want to build will be. We will use a medium sized breadboard with enough room for our power supply, the microprocessor, and the LEDs we want to control.

Step 2: Starting With the Power Supply

We will use a voltage regulator called a 7805. It has three pins, the first is the input, next is the ground, and last is the output voltage... in this case 5 volts. The chip needs to have greater than 6 volts to be able to regulate it down to 5 volts... but it cannot have more than 36 volts. The 9 volt battery works well in this application.

We also want to install a diode into the circuit... it will allow current to flow in only one direction. The reason we install it is so that if we connect the battery backwards it will not let the current flow and will protect our circuit from damage. The diode has a line printed on it, this should be on the most negative side, in our case... it connects to the input of the voltage regulator.

The 9 volt battery clip uses stranded wires... they do not plug into a breadboard very well so we need to make it so that we can install the connections properly. We will use what is called a SIP header. (Single Inline Pins) The wires are soldered to the pins and then can be inserted into the breadboard.

If you look at image #3, you can see that the wires are soldered to TWO pins. This is done to provide a stronger connection that won't turn or jiggle loose. (It's a good tip to remember! )

We insert the pins into the breadboard and install the diode. (Image #4)

In Image #5 you can see that we have added a capacitor... it is a 47uF rated at least 6 volts... these are 25 volts... so they are fine. They act to smooth out the voltage when things like LEDs turn on and off. One way to think of them is like the water tank in your toilet... when you flush you need a large flow of water all at once... but the supply line is very small. The tank holds enough water to even out the flow. Likewise, the capacitor holds extra power for when there is a surge from things starting and stopping.

One capacitor goes between the input to the voltage regulator and the ground, the other goes between the ground and the regulated 5 volts. The capacitor has a marking to show which side connects to ground.

In Image #6 and Image #7 you can see the completed power supply.

Step 3: Wires and Connections

You will notice that my connections to the board are very neat and easy to follow. (At least I hope so!) I am using a wire kit that is available online from several places. The kits are about $10 to $20 depending where you look... and refills are available from places like Digikey .

The kits make your wire connections short, clean, and easy to follow when you come back to them in the future. The refills are a bit pricey, but if you get into electronics quite a bit you will find that you don't want to live without them.

A great way to get started is to find some 22 gauge to 24 gauge SOLID wire. You don't want stranded wires in a breadboard... they just don't work well. Solid wire is useful when you want it stiff and bendable as we are doing here. Stranded wire is used when it must be flexible.

If you see the local telephone repair person ask if they have any scrap 25 pair cable. They often get it in 500 foot rolls... and if it has less than 20 or so feet will often throw it away. The cable has a string that when pulled will cut the jacket and give you a whole bunch of wires as shown in Image #3 .

Step 4: How Do We Know If It's Working???

So now we have a power supply for the microprocessor and out LEDs. But how do we know if it is working? We can install an LED so that when the power supply is on it will illuminate. This is also a good time to teach you about resistors. We will be working with 5 volts... but an LED is designed to run on only 2 volts. (Approximate, they vary depending on the type. )

If we were to connect a 2v LED to the 5v supply, it would get very bright for a very short period of time... then POOF! Here is where you need a little math... don't worry... it's not too bad.

If we look at the specification sheet for the LED, it says that it runs on 2 volts at 20mA. Okay... let's start with the voltage... 5 volts minus 2 volts is 3 volts. So we have 3 volts too much for the LED. (Told you the math wasn't too hard.)

Okay... the LED runs on 20mA... that's the same as 0.020 amps. (mA are 1/1000 of an amp... so 1000mA = 1A) We know that we have 3 volts too much... so divide that by the current that the LED is supposed to run at...  3 divided by 0.020 = 150.

So we know that we need to have a resistance of 150 ohms to slow down the flow of electricity to a point where it won't burn out the led. Voltage / Current = Ohms.

We install our resistor (150 ohms) and our LED onto the board... now we know if we have power or not!

Step 5: Adding the Microprocessor

We now have a working power supply, we understand how a breadboard works, and we have the wires... let's connect up a microprocessor and do something!

On order to make the microprocessor do anything, we need a way to get the software into it. There are four signals that send the data to and from the microprocessor during programming, they need to be connected to a programmer. If you have a PC or Laptop with a parallel printer port, the BASCOM software we will be using later has plans to make a very simple programmer. If you only have USB ports, then you can purchase a USB programmer. There are links on the BASCOM page.

To connect your programmer to the microprocessor, there is usually a 6-pin (or sometimes an older 10-pin) connector. The folks at Sparkfun have made a great adapter as shown in images #1 and #2. Once you solder in your pins, simply plug it into the breadboard and connect your wires. You can get yours here:

Image #3 and #4 show how we are installing the Tiny-26 microprocessor into the breadboard. Note that we rotated the breadboard 180 degrees so the text would face correctly in the photographs.

Next we connect the power and ground connections, then make the jumpers for the following signals from the Sparkfun board to the programming pins of the chip.

* MISO - Data into the chip from the PC.
* SCK - System Clock and timing.
* RESET - Reset, tells the chip to enter programming mode.
* MOSI - Data out form the chip to the PC.

We also have connections for power and ground. Image #7 shows where we want to install the programmer, Image #8 shows the connections, and #9 shows it all assembled.

Image #10 shows the names and connections of the Tiny-26 Pins. You can see that we connected MOSI to MOSI on the chip for example. If you get MOSI and MISO reversed it won't work... not that I ever did that. :-/

Step 6: Connecting the LEDs

We need something to control with this microprocessor... let's add three LEDs to the circuit.

This chip has two ports named PORTA and PORTB as can be seen in image #1 . (Look familiar?)

We could have connected the LEDs to PORTA , but I wanted to show that you can use the programming port to also control things. Connecting something like a motor controller to the programming port would not be a good idea... the motor would turn on and off uncontrollably during programming. But with the LEDs connected, you will see them blink as we program the chip. I just think that looks kool too.

The chip can support up to 20mA per pin... so a single LED on the pin is fine. If you wanted to connect multiple LEDs to a pin, then you would need to install something to drive the needed power... like a 2N2222 transistor or similar. (But that's for another Instructable!)

If you look at the remaining images you will see that the LEDs are connected and the resistors are installed. Remember that the LED needs to be installed correctly, the FLAT side is connected to ground via the resistor.

Step 7: Programming the Chip

There are many programming languages to choose from for programming the Atmel series of chips. Some people like to use assembly, others prefer C. I have been programming in BASIC since 1978 so I like to use that language. There is a GREAT version of BASIC for the Atmel that is very powerful and easy to learn, it's called BASCOM . You can download it and get more information here:

The demo version will allow you to program up to 4K of memory space... and since this is a 2K microprocessor... that will never be an issue. When your programs get bigger and you migrate to more powerful chips, the program only costs about $80 which is a real bargain for all it does.

Once you install BASCOM , the screen will look something like image #1

Image #2: Select options, compiler, then chip. a menu screen will open up.

Image #3: Select the TINY26 from the list. then click the ADD TO CODE button which will add the commands tot he code so that you won't have to keep selecting the chip type. It defaults with a speed of 4MHZ for the crystal... and needs to be changed to 1MHZ since we will use the internal clock of the chip. The line should read...

$CRYSTAL = 1000000

Image #4: Here you can see the code that was generated. It tells the software what kind of chip is selected, what speed we are going to run it at, and it has some other (optional) data to define how the hardware is configured. Once this is in the software, it knows everything needed to program the chip. It wouldn't do anything we would call useful... but it would program okay.

Image #5: This is our program... let's go through it.
$regfile = "attiny26.dat"
$crystal = 1000000
$hwstack = 32
$swstack = 8
$framesize = 24

Config PORTA = Output
Config PORTB = Output



Red = 1 : Yel = 0 : Grn = 0
Wait 1

Red = 0 : Yel = 1 : Grn = 0
Wait 1

Red = 0 : Yel = 0 : Grn = 1
Wait 1

Goto Begin

The first section sets up the chip, then we need to configure the two ports. A port can be an INPUT or an OUTPUT. Since we want to run some LEDs, we set the port to be an OUTPUT. May as well define them all at one time... so we did.

The next section is where we define the pin names. I don't know about you... but I would forget which pin the RED LED was connected to, or the green, or the yellow. I don't feel like typing in PORTB.0 for the first pin every time... so we told the software that it's name was "RED". Now all we need to do is reference it by it's name.

Once defined, if we make them equal a "1" the LED will turn ON, and if we make it equal to a "0" it would turn OFF. The next series of lines defines how we want the LEDs to be set, then waits 1 second. (The WAIT command.)

After we change the state of the LEDs 3 times... we jump back to the beginning and do it all over again... over and over.

Image #6: To get the software into the chip we must first COMPILE it into something it understands. Clicking on the black chip will run the compiler... this makes a HEX file that can be loaded into the chip. If there are any errors they will be shown at the bottom of the screen and you will need to correct them.

Image #7 : When you click the green chip, the programmer opens up. If the chip is connected properly, the programmer screen will display. If not, it will say that it can't find chip FFFFFF and you will need to correct the problem.

Image #8: Once you get the programming screen to show up, simply click the green chip on that display and the program will be loaded into your chip... once finished, your chip will start running your program. You can disconnect the PC or Laptop and your chip will run your program all by it's-self.

Step 8: It's Alive!

At this point your microprocessor will be running the LEDs in sequence...

Red --> Yellow ---> Green ---> Red...

They will each light for 1 second. If you change this...


to this...


Then it will blink every 100 milliseconds (1/10 of a second.)

Can you change the code to make a traffic light? (Hint, change the time settings. )

With the addition of only 2 more parts we were able to have an LCD screen running. You can see some images of that on my BLOG page .

Stay tuned for more information, downloads, and videos. If you like... visit me at to see my tutorials, blogs, and more.


Just in...

I got with Mark Alberts (author of BASCOM) and asked him which USB programmers he recommended for use with BASCOM. Here are his favorites.

1) Atmel AVRISP mkII In-System Programmer (ATAVRISP2)
2) USBasp - USB programmer for Atmel AVR controllers

So get one and dive in!

Microcontroller Contest

Participated in the
Microcontroller Contest