ATMega328 Led Blink + Fan control (PWM) Project (In progress)

Go To Last Post
13 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello everyone,

After lurking on this forum for years now (I'm currently on my senior year) I decided to create an account since I need help / 2nd opinions or suggestions for my big final project. I'm making a basic project with an ATMega328:

---Brief Explanation---

I need to use an ATMega (in this case, I chose to go with the 328p version + an external 16Mhz crystal) and create a program (and build it) where I have a 1Hz square wave output (I connected a LED in order to simplify this step - 500ms the led is on / 500ms the led is off) + a basic PWM control (I'm thinking about using a fan / motor in order to change it's speed via PWM - i.e. pressing switch 1 will increment pwm in 10, pressing switch 2 will decrement pwm in 10, pressing switch 3 will force pwm to 50, pressing switch 4 will force pwm to 100, pressing switch 5 will force pwm to 0).

At the moment I (think) I've made the first step and completed the led / square wave output code (image below). I also built my circuit on a breadboard (image below) aswel as in a schematic (didn't simulate, didn't had the time yet; image below). If this is alright, I'm missing the PWM part and that's it.

My issue here is:

Since I can only test it next week, is the code fine (schematics aswell)? I can't seem to understand how the debug function on Atmel Studio works (my last time messing around with microcontrollrers was 1 year ago. Since then I had 1 or 2 projects in arduino so I kinda forgot some basic ATMega things).
Also, it would be helpful to replace the voltage source (10V in the beggining of the circuit) with some sort of battery, etc. so I could experiment the circuit at home. Any idea if that's something hard to find / implement?

 

Last Edited: Mon. Sep 30, 2019 - 10:35 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You need bypass caps on ALL the AVR Avcc and Vcc pins.

The crystal needs to be at the AVR...not the way you have it

 

'Counter500' needs to be declared VOLATILE as it is shared between init and an ISR.

 

You have not shown us your MAIN loop.

 

I am sure others will chime in as well.

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Are you kidding?  Where did you get this drawing?---ask for your money back!  The schematic should show the components connected together (that's what a schematic is)....why would the crystal be somewhere else?  The led is an inch away from the AVR & you neglect to show the connection???

Why not move power LED D3 to page 2?  Well, because it belongs with the power supply, as shown...The same idea applies to ALL parts.

 

Move the Vcc & Avcc pins to near the top of the chip symbol, so the power wires don't cross in front of ALL of the other pins. 

 

with some sort of battery

Forget the battery...use a 9-12v decent wall wart feeding your 5V regulator   (do NOT use the wall wart as the regulator, keep the 7805 as-is)...have hours of fun

 

The crystal hookup is terribly improper ...all wires associated with the xtal & its 2 caps should connect only & directly to other avr pins & never with more than an inch or 2 of wire, if a direct (no-wire) connection is impossible..   

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Thu. Sep 26, 2019 - 11:27 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:

You need bypass caps on ALL the AVR Avcc and Vcc pins.

 

What do you mean by byspass caps? Probably don't recognize it because of translation issues but still... What does it do and is it wrong by not having them this way?

 

jgmdesign wrote:

The crystal needs to be at the AVR...not the way you have it

 

I THINK the micro has a crystal inside but some school mates said that he didn't. Tried searching for it on the datasheet but as I was in my phone, couldn't find any reference to that. Nontheless, is there an issue by having an external crystal?

 

jgmdesign wrote:

'Counter500' needs to be declared VOLATILE as it is shared between init and an ISR.

 

Alright, will do it.

 

jgmdesign wrote:

You have not shown us your MAIN loop.

 

I wrote the main function in the code. It's simply calling out init and looping it since I'm yet to take care of the PWM things.

 

 

Last Edited: Fri. Sep 27, 2019 - 12:33 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think I explained it wrong. This drawing was made in class (it's basically a sketch) and it was basically for us to understand what connections would require. I know it's not a schematic in it's form (my bad, shouldn't have called it like that). I'm not interconnecting wires or something like that. Nontheless, I will have some spare time tomorrow and send the real schematic through PSPice / Orcad. 

 

I have no idea what a wall wart is (translation issues, google doesn't help either) so mind linking me one to buy / make? 

 

When you talk about the crystal, are you mentioning the shematic montage or the real life montage? Because you can forget the schematic (like I said above, was my bad) but the montage IRL is the deal.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

the hook up is criticl when you wire the crystal...direct or at the VERY most 2 inches.

 

Wall wart is just the square power blob you plug in the wall for your answering machine, blood monitor, etc.

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Fri. Sep 27, 2019 - 12:40 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So what would you recommend me doing about the crystal? How should I put it on my BreadBoard?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Plug the crystal pins into the xtal pins on the avr...  Plug in the caps too & the caps gnd into the avr gnd pins if possible....no wires used 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The problem is I need to connect the crystal into 2 followed pins (like shown in the schematic). It doesn't fit.

EDIT1: Nevermind, I was scared of breaking it. Im dumb. I got what you said, I will do it tomorrow and re upload the picture.

Last Edited: Fri. Sep 27, 2019 - 12:48 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Jonaas18 wrote:

jgmdesign wrote:

You need bypass caps on ALL the AVR Avcc and Vcc pins.

 

What do you mean by byspass caps? Probably don't recognize it because of translation issues but still... What does it do and is it wrong by not having them this way?

Ask your instructor.  It's a fundamental requirememnt to place bypass caps as close to the AVR/integrated circuits to reduce noise on the power lines.

 

jgmdesign wrote:

The crystal needs to be at the AVR...not the way you have it

 

I THINK the micro has a crystal inside but some school mates said that he didn't. Tried searching for it on the datasheet but as I was in my phone, couldn't find any reference to that. Nontheless, is there an issue by having an external crystal?

The AVR runs on an internal RC oscillator that is divided by 8 to produce a 1Mhz internal clock.  YOu se the fuses at programming for an external crystal clock source.

 

jgmdesign wrote:

'Counter500' needs to be declared VOLATILE as it is shared between init and an ISR.

 

Alright, will do it.

 

jgmdesign wrote:

You have not shown us your MAIN loop.

 

I wrote the main function in the code. It's simply calling out init and looping it since I'm yet to take care of the PWM things.

This will not work as you are contantly resetting up the AVR as opposed to sitting in an endless loop waiting for interrupts to fire.

 

 

 

I suggest you first blink an LED before you start getting into PWM fan control.  Also work on port bit manipulations and reading a port, then writing the read to another port.

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

 

So, not a great example, but it illustrates two points.

 

The Crystal is adjacent the micro, and is plugged in by the Xtal pins.

It isn't placed somewhere else on the breadboard.

 

In the circuit above, it could / should be placed in the holes directly adjacent the micro's pins, but using the 2nd, or 3rd out pins didn't matter in this case.

The Xtal's caps connect to the Ground rail.

If the caps have long leads then cut them to 1/2 length or even shorter.

 

Above the microcontroller chip there is a small dark red / maroon colored capacitor, (0.1 uF).

It goes from the Vcc pin to the Ground rail.

Ideally it should be plugged into the hole adjacent the micro's Vcc pin.

 

This chip has Vcc and Ground on opposite sides and at opposite ends of the chip.

Many of the "newer" chips have the Vcc and Ground, and Analog Vcc (AVcc) and its Ground, pins, side-by-side.

 

All of the Vcc and the AVcc pins need a "By-Pass" cap, typically 0.1 uF, across the Vcc/Ground and AVcc/Ground pairs of pins, and they should be as close to the chip as you can position them.

 

The little temperature sensor chip, (that looks like a little transistor), seen to the left of the Pot, shows the (yellow) cap mounted right next to the device's power supply pins.

 

Good luck with your project!

 

JC 

 

Edit: Typo

Last Edited: Sun. Sep 29, 2019 - 12:07 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That's a nice breadboard setup...shows the newbies a few things!

It's about that temperature here, almost October & the AC ran today.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

:: Morten

 

(yes, I work for Atmel, yes, I do this in my spare time, now stop sending PMs)

Topic locked