No Arduino Uno. No Arduino IDE.

Ditching the Arduino IDE
20150906_181919

Some time ago, I started playing around with the Atmega328p microcontroller without the Arduino Uno board. Now it’s time to try doing the same on the software side, even as just an experiment. I’ve been getting started with AtmelStudio which allows you to program Atmel microcontrollers in C. I’m still not quite clear on what to call the Arduino programming language other than C, if not a simpler form of C because the Arduino language takes care of a lot of things for you to make it as easy as it is to program an Arduino.

I learned about microcontroller programming in college. I believe we used a PIC microcontroller for the labs so, while I recognize certain things, there seems to be a little variation in abbreviations. In the labs, the microcontrollers were part of a board my professor designed that had a bunch of peripherals like buttons, switches, LEDs, and an LCD. It was a very thorough course with loads of information to take in. I hope I can remember it all!

Anyways, I’d really like to continue playing around with it and see if I can make a full project with one of my Atmega328p boards programmed using AtmelStudio. I’m taking baby steps to get up to a good speed in AtmelStudio, and I hope that writing small tutorials on the blog will help me firm up my knowledge. If you spot anything that needs correcting, please leave a comment!

The rest of this post will introduce you to the little work I’ve done so far, and may be helpful for anyone else who may be interested in trying it out too.


Hello World!

I made two Hello World programs, with the help of a bunch of guides which I will link to throughout the rest of this post. Both programs blink a bunch of LEDs but using different methods. One uses a delay, while the other uses an interrupt routine.

USBtinyISP with AtmelStudio

AtmelStudio does not work right away with the USBtinyISP, which is what I used to program the Atmega328p. You must add it as an external tool. I followed this guide to add it to AtmelStudio. You’ll need to download WinAVR to complete the guide.

As the guide says, to upload code with the USBtinyISP, you must go to Tools>USBtinyISP (or whatever you named it). Just remember to build your code first before uploading it.

Code

Using a Delay (and Beginner’s Introduction)

As I mentioned, I made two programs that blink LEDs. One uses a delay while the other uses a timer interrupt. Let’s first take a look at the delay version.

Screenshot (3)

The first line defines the clock speed, which is 16MHz. The next line includes io.h header file, which actually just includes the appropriate file for the project’s microcontroller (which you select when you create the project, in this case the Atmega328p) that contains definitions for register addresses and things like that. The third line includes delay.h header file which enables us to use _delay_ms(); to cause the delay.

Now, into the main program! But first, let’s talk about ports.

atmega328p_pinout

This is the pinout of the Atmega328p-pu. Some of the pins are labelled PBx, PCx, and PDx, where x is a number. These stand for PORTB, PORTC, and PORTD, which are 8-bit data registers where each bit is a pin. The pins on the uC aren’t exactly laid out as cleanly as an Arduino Uno when looking up and down the rows of pins, but you can always order them in a way that works best for you when you’re actually wiring up your project.

Anyways, back to the code. In the first two lines of code in our main function, we set all of the pins in PORTB and PORT D to outputs. This is equivalent to the pinMode() function of the Arduino which is used to set certain pins as inputs or outputs. DDR stands for Data Direction Register where setting a bit/pin in the DDR to 1 makes it an output and setting it to 0 makes it an input. To make things easier, I put it in hexadecimal format (“0xFF”). You could also write “0b11111111” which is the binary equivalent and is more helpful when pins in a port are a mix of inputs and outputs. The third line in the main function sets all of the bits in PORTD to 1.

Everything in them main function up to this point is like the setup() function in the Arduino IDE. This code only runs once when the uC is powered on. Everything in the while loop will loop forever, like loop() in the Arduino IDE.

We set all of the bits in PORTD to 1 earlier so that we can toggle all of the bits on PORTB and PORTD at the same time and they will alternate. If we didn’t set them before we started the loop, all the bits in both ports would be the same. In another case, we could put in some logic that would toggle or set all of the bits in PORTD to 1 on the first loop, but that’s unnecessary since we could do it this way.

The exclusive OR (abbreviated as XOR, symbolized as ^) operation can be used to toggle bits high and low whenever the line is executed. In this code, the bits in PORTB and PORTD are toggled, and then there is a delay set for 250ms on the last line of the while loop. It loops around and toggles all of the bits again, and so on.

Using an Interrupt

A lot of the core stuff was covered in the previous code section so I’m not going to go into as much detail for any repeating lines in this section. Also, I’m rusty when it comes interrupts especially so don’t quote me on anything. Check out this and this for the information that I used to help me get this working. Also see the Atmega328p datasheet.

This program sets up Timer1 interrupt so that the ISR, or Interrupt Service Routine, will execute every 250ms. Let’s go line by line where we initialize Timer1.

cli(); disables interrupts. This is used so that important code that shouldn’t be interrupted isn’t.

The next two lines set all of the bits in the TCCR1A and TCCR1B, or Timer/Counter Control Registers for Timer1, to 0.

See the large comment block for information on calculating the Output Compare Register (OCR1A) (“Compare Match Register” in the comment block).

The next line enables the Clear Timer on Compare Match mode, which clears the timer once a match is made.

The next two lines are to set the prescaler to 1024, which is a value that was used to calculate the value for the Output Compare Register. The following table, found in the Atmega328p datasheet, shows how to set the bits in the TCCR1B register to set the prescaler.

Screenshot (6)

The next line enables the Output Compare A Match interrupt.

The last line enables interrupts.

There’s nothing in the while loop since all of our code is in the interrupt routine.

The bottom of the code is the Interrupt Service Routine (ISR) which is the code that runs when there is an output compare match with Timer1. In the ISR, we toggle the bits on PORTB and PORTD like we did in the other Hello World example. The ISR code is run every time there is an output compare match, which is set to 250ms.


So, uh, that’s it.

Trying to write this post was difficult. I need some more reading up and experimenting to do. I hope you got something out of this, at least a link to somewhere that’s more helpful.

Thanks for reading!

 

Advertisements

Manufacturing Session

There’s something satisfying assembling a PCB, even more when you assemble and end up with multiples of the same design. I received the last parts to assemble the remaining 5 Attiny85 Programmer/Breakout PCBs.

IMG_20140908_150710While waiting for the parts to arrive, I bagged each board with its components and headers. The headers are not part of the assembly in case you wanted to solder wires directly to the board. Soldering on the other parts was simple enough.

Once the boards were complete, I did a simple programming test with each one. They were all able to upload and run the blink sketch.

IMG_20140908_150658In other news, I decided not to assemble the shift register boards. If people are interested in them, I’d just cut my losses with this version and get the new one manufactured, though it wouldn’t really be a complete loss since I can always find a project for them in the future.

So that’s it for now. Another blog milestone is coming up next… See you then!

Attiny85 PCB Rev B Preview

Moving along with some revisions to my first two PCB designs, I’m working on some changes to the Attiny85 programmer & breakout board. The following is sort of a mock up as I’m just trying to figure out the form of the board right now.

1So the first thing that you’ll notice is that it’s not a square. It has rounded corners this time! But really, the form of the board has changed a lot. I want to design it so that you can break off the programming area when you’re ready to use the board in your project. The benefit to that is the breakout takes up less space in your project… and it’s kind of neat, IMO.

Something else I did new this time is that this board is actually completely manually routed. I’ve relied mostly on the autorouter up to this point. The Design Rule Check isn’t coming up with any errors so that’s positive.

Some parts should be coming in any day now that will let me assemble and sell the first version of the Attiny85 Programmer & Breakout on eBay, along with the other things I’m currently selling. People buying them will show me how much interest there is in this kind of thing. I really like this design I’m going for so hopefully there’s enough interest to get the new version manufactured when it’s ready (Rev A buyers may get a discount). Stay tuned! Thanks for visiting!

EERef Version 2.1 now available!

I think I’m rushing updates too much so I think I’m going to take a break from EERef…

Anyways, I’ve added two new tabs: Arduino and Pinouts. They’re both reference material that I think is handy to have for quick offline access. I tried to make it so that you get the information you most likely need without having to scroll through pages of a datasheet.

The other “new” thing is that there will be an update notification when the program starts if there is an update available. It’s a little more aggressive now because I feel it’s more important to get updates out with the new pinout material. (Yes, I know people don’t care much about this program to even think it has something like update notifications, but let me play a little.)

Get it now!

 

7-Segment Counter Prototyping

I’ve been fighting with myself about getting some LED dot matrices… but I just know they’ll end up collecting dust like my 7-segment displays. With that, I thought I’d do something with one of them to distract myself from buying those matrices (for now). I decided to make a simple counter that counts up with a button.
I used a couple of shift registers so that I could use an Attiny85, sort of a combo of things I’ve been playing around with these past few weeks. I finished prototyping it on a breadboard so I’ll be planning the perfboard and soldering it all up!

Tutorial: Shrinking your Arduino projects

Introduction

I got my first Arduino a couple of years ago but it wasn’t until recently that I tried making a standalone circuit with just the microcontroller. It sounded intimidating to me before I tried it but now I look forward to seeing my projects break away from the Uno board. There are many resources out there that can help you do the same, but I wanted to throw in my vote of confidence and show you the method that has worked well for me so far.

Requirements

Arduino Uno board

Yes, you’ll need one if you’re going to follow this tutorial. This tutorial is geared toward people who have already been playing with their Uno and want to shrink a project. The Uno will act as our programmer. You can program the chip directly using other methods but I can’t speak of them since I haven’t tried them.

Atmega328p Microcontroller

There are other Atmega chips out there so make sure you’re using this one if you’re going to follow the information here. Also, get one with a bootloader already on it. I purchased mine from Dipmicro which has the Optiboot bootloader on it already. You can get the other one with the Duemilanove bootloader, but make sure to select “Arduino Duemilanove w/ ATmega328” when you program using the Arduino IDE.

Breadboard

You can’t have too many breadboards. I’m sure you already have one sitting beside you right now.

Capacitors

We’ll need two 22pF (22) and one 100nF (104) capacitors.

Crystal Oscillator

We’ll need one 16Mhz crystal oscillator.

Resistors

We’ll need one 10Kohm resistor. We’re running the blink sketch so we’re going to need another resistor for the LED.

LEDs

One LED for the blink sketch. A power indicating LED is optional. Just remember the current limiting resistor!

(Optional) Voltage Regulator

The Atmega328p chip can handle 5v only. If your power supply gives you more than that, you’ll need a regulator to drop it down to 5v. Check out my LM317 tutorial for one solution.

(Optional) Push Button

If you want a reset button, you’ll need a push button. I personally don’t include them because all of my projects have an on/off switch that I could just toggle.

Connecting the Circuit

You can find the pinout of the Atmega328 chip from the datasheet. The chips that I get already have a label. There are places that sell the labels if the chips that you get don’t have them already. They’re great to have because there’s no use for a schematic. It’s really that simple.

IMG_0877

I don’t have a schematic to share but a quick search on Google will get you one. If you’re good at following text instructions, it’ll be easy:

1. RST has a 10Kohm resistor to 5v.

2. The 2 Vcc’s on both sides and REF go directly to 5v.

3. The 2 GND’s on both sides go directly to ground.

4. The 100nF capacitor connects Vcc and GND on the left side of the chip.

5. X1 and X2 go to ground using the 22pF capacitors.

6. The 16Mhz oscillator connects X1 and X2.

IMG_0869I don’t have any larger breadboards to show  you how to connect it up, but this small breadboard is a great example of how small you can get the controller to be.

Programming

Like I said earlier, we use the Arduino Uno board to program. Just remove the current chip, insert the one you bought for your shrunken project, and program as you normally do. It’s a good idea to unplug the board when you’re swapping chips. Once you get comfortable, you won’t even have to prototype on the Arduino Uno. You’ll start using it just for programming. There are other ways to program the chip directly but I haven’t tried them (yet). This way just seems the easiest considering I already had the Uno so I won’t need to buy anything more.

Conclusion

So yeah… There are guides out there that may be better to follow with more graphics. Once you do it once, you’ll never go back to sharing around your Uno between your projects. I just wanted to show off my method and encourage people to try it!

Temperature and Humidity Sensor Project Part 1!

I’ve raved about these guys many times in my past posts, and it continues! I got another order in today from Dipmicro. They consistently ship out by the end of the next day and the goods arrive three days later for me (standard, $3 shipping). Considering my increasing intolerance for eBay and things going through customs, finding this company is gold to me. Check them out if you’re in Ontario.

IMG_20140109_161539So what goodies came for me today? The purpose of the order was for the temperature/humidity sensor. I started thinking that the air was getting dry in my house recently (I woke up on new year’s day with a nosebleed) so I wanted to monitor it. That’s where the 7-segment displays come in. Of course, I was drawn into the LED section again and so I started thinking of another revamped light show. I will be experimenting with some shift registers, along with 20 new RGB LEDs. I also picked up some resistors, capacitors, and a couple of prototyping boards now that I’m not afraid to use my soldering iron.

Now back to the temperature/humidity sensor project. The original plan was to make a small module that I could kind of “plug and play” since I only have this Arduino Mega which will be shared between this project and a new light show. I want to be able to plug it back in at any time, reupload the code, and see the values read out on the 7-segment displays. Since I had some time restlessly waiting for the shipment to come in, I decided to try writing a Visual Basic program that would do the same thing as the 7-seg displays.

IMG_20140109_221229The set up was easy. There’s a tutorial on the Adafruit Learning System.

ConnectionLogThe program launches with this Connection Log window. It goes through possible serial ports until it connects to the Arduino. If it doesn’t detect the Arduino, the program just quits. Half of my time spent on this program was just working on error handling. If you’re in the main program and the Arduino disconnects, it will reopen this window and try to reconnect instead of crashing into a fiery ball of sadness. It gets a little wonky if the temperature/humidity sensor circuit is broken so you have to quit the program, and disconnect and reconnect the Arduino. It’s not the way I wanted to handle that error but that’s how things worked out.

interfaceThe VB program grew from a couple of text boxes into this, and I’m really happy with it. The only issue is that it’s laggy when you try to drag the window around, probably due to the amount of things I have it doing every second. It’s still usable, though.

debugThis is how the program started out. The Debug tab can be enabled in the File menu. It was just to test that I could send and receive between the Arduino. I didn’t really understand the concept when I tried this before so I was really excited when I got this working. You can bet this will be part of the new light show.

Now, there is a part 2 to this project, and that is the hardware. I want to put my new soldering iron to use in this one so I’m going to set up my two 7-segment displays on the protoboard along with the sensor and hopefully my original plan works out. I’ll post about that soon. Thanks for reading!