How to use total 21 pins (9 input pins and 12 output pins) in Atmega8

Hello, I am going to create a small project with few buttons and few led's. I want to use 9 pins for buttons as inputs and 12 pins are for LED's as output.

I am confused with the pinout of Atmega8A and I noticed only few pins are direct I/O pins and I don't know how can I use remaining pins as normal digital I/O pins. Please help me on this.

What do you mean only few pins are direct I/O? Almost all are I/O which is the default functionality. There are 23 in PDIP version, if I count correctly.
Take look at the datasheet. There are PB0-7, PC0-6, PD0-7.

Start with the Analog pins, they can be analog in, digital in, digital out depending on your code. If you are using I2C you will lose two of them,A4/A5. Now you should have enough pins.

What is the code if I want to use analog pins as digital in and digital out pins?

Use the "pinMode(); in your setup. Before progressing I highly recommend you get and read a copy of the Arduino Cookbook, It will answer the questions asked and even give you examples.

1 Like

The same as for digital pins; just use the pin number Ax (where x is the actual number).

To get 23 IO, you need to use the internal clock to free up the external crystal pins, and reprogram the fuse that allows RESET to be a gpio. In normal Arduino code, you’d only have access to 20ma IO pins (and two of those are normally the serial port used for uploading sketches.)

Fortunately, you don’t really need 9 pins to do 9 buttons, or 12 output pins to control 12 leds

If you don't want to detect multiple buttons at the same time, you can use an analogue pin with a resistor ladder for the buttons. this way you connect all 9 buttons using a single pin.

He did not write 9 buttons. He wants to use 9 pins for buttons of unknown amount. :slight_smile:
Same with LED's.

It is not limited to 9 buttons. The point is that he can use a single pin for multiple buttons.

How come?? is there any example code that single pin can use for multiple funtions?

Try to read this:

and many other examples if you google for it.

Maybe you could share something more about your project to obtain better answers.

I am so confused with your example.

following is my simple requirement:

I want to use 4 pins i.e. 2,3,4,5 as inputs on arduino UNO

whenever I connect the pin#2 with GND pin, I want to turn ON the RED LED on pin #8 like wise
whenever I connect the pin#3 with GND pin, I want to ON the BLUE LED on pin #9
whenever I connect the pin#4 with GND pin, I want to ON the GREEN LED on pin #10
whenever I connect the pin#5 with GND pin, I want to ON the YELLOW LED on pin #11

Note: I connected each LED with respective pin with GND pin on arduino

is there any way to use single pin to control all the LED's for different input pins?

My post is about the buttons. Read the post #8 by @jobitjoseph carefully. It have to be analog pin. Basically, you will read the voltage on input, based on which button is pressed.

oh BTW do you have any idea on my requirement?

No current limiting resistor?

LEDs will need separate pin each. But there is enough for your need. Red, blue, green, yellow, that is 4.

Yes I added 680ohm current limit resistor for each LED

Schematics would help better than 1000 words.

I need total 22 pins and I want to avoid Tx and Rx pins