Connecting microcontroller


picture of microcontroller with led

I'm trying to glow an led using a microcontroller and switch. I have programmed it to blink
Every 2 seconds.when voltage comes from PB1 switch gets tuned on and led . Circuit diagram is shown here.
It doesn't work. Please help me to fix this how to connect to atmega 8?

Best Answer

Ok, first we need to tackle that circuit of yours.

I think you're probably trying to run before you can walk, since there are a large number of things fundamentally wrong with it - things that you really should know before you start playing with microchips.

  1. LEDs require a resistor (or constant current supply) to limit the current flowing thorough them, or they will blow.
  2. Similar to an LED, the base of a Bipolar Junction Transistor requires a current limiting resistor or too much current will be allowed to flow out of the IO pin, and they have a finite limit of what they can handle (\$40mA\$ absolute maximum, \$20mA\$ recommended maximum).
  3. All chips require decoupling capacitors on their power pins. These absorb the ripple the chip itself causes, and also act as a little reservoir of power close to the chip .
  4. It looks like you have a floating ground pin there. All ground pins on a chip must be connected to the ground rail.
  5. Unless you have specifically disabled \$\overline{RESET}\$ in your fuses you will have to connect the \$\overline{RESET}\$ pin to +5V using a \$10K\Omega\$ resistor, or the whole chip will just sit there doing nothing.

The formula for calculating the resistor for an LED is:

\$R_{LED} = \frac{V_{IN} - V_F}{I_F}\$

Where \$V_{IN}\$ is the supply voltage, \$V_F\$ is the forward voltage drop of the LED, and \$I_F\$ is the current you need to flow through the LED.

Taking some typical values of \$V_F = 2.2V\$ and \$I_F = 20mA\$ we have:

\$R_{LED} = \frac{5 - 2.2}{0.02} = 140\Omega\$ - or the nearest easily available resistor above that value (\$150\Omega\$).

The base resistor's value is less critical, and anything in the region of \$470\Omega\$ should be fine.

Decoupling capacitors would typically be \$100nF\$ per power pin pair, and often \$10{\mu}F\$ per group of around 3 chips or so.


simulate this circuit – Schematic created using CircuitLab

Now, once you have all that sorted, and possibly have replaced your LED which may by now not be functioning, we can look at your software. If it's still not working after you have made all the right changes, then please post your full source code so we can see if you have configured the chip correctly.