Multiple switch with multiple Leds

I've got a problem with my arduino project. I want to make a circuit with 10 switches and 10 Leds and when I press the first switch, I want the the third led to light up and when I press the first switch again, I want another led like the sixth one to light up and so on for the other witches and leds. I want to switch between all the leds for each of the switches.
Could you please tell me how to wire these on a breadboard and code them on arduino?

Leds wired as normal from a digital pin to ground with a series resistor, pinMode()-ed as output

Switches wired as normal from a digital pin to ground with a pullup, ie pinMode()-ed as input_pullup.

If you have a Uno, that will need all your pins, including 0 and 1 which are also needed to upload the code.

Before you get too involved in this, have a look at how pins can be managed with arrays, else this will rapidly become a pitfa.

I want to switch between all the leds for each of the switches.

Randomly or according to some defined sequence?

Thank you so much

If you're very new to Arduino you should work through the examples which ypu can also access in the IDE at File > Examples.

Actually I'm very new to Arduino. Thank you for your help!

I'm very new to Arduino.

My advice then: leave the project on the back-burner for a while, while you find your feet. Of course, if you keep the project in mind, as you work through the examples you will have a few Aha! moments, so mentally bookmark the things that look useful... but don't get into the actual project too early. YMMV.

Got it! Thank you so much for your help again!

Just another question. Can I do this with 26 Switches and 26 Leds? Will there be enough place to wire them onto?

Can I do this with 26 Switches and 26 Leds?

Not on a Uno which has 20 pins, unless anyone suggests some kind of multi-plexing approach which is above my pay-grade.

Look at a Mega, which has something in the 50s, I forget the actual number.

Got it. Thanks again! :slight_smile:

Yes and no.

Yes you can do this.

No, you can't do this with just an Uno and straight connections to leds/switches

Yes, you could do this with something like port expanders. Would say that's a cleaner approach than a Mega.

Yes, alternatively you could multiplex everything.

Yeah. As meldown said I have to use a mega. Thanks for your help.

Mega2560 has 70 IO pins that can be used for inputs & outputs.

Thanks for your details! :slight_smile:

Yeah. As meldown said I have to use a mega. Thanks for your help.

No, you do not at all!

Megas are cumbersome things, and unnecessarily expensive. In fact, UNOS are a poor form factor for projects such as you are formulating, Nanos a much more useful form or if the project will never require re-programming, a Pro Mini.

If indeed, this is only about switches and LEDs, the answer is to use matrix arrays and multiplexing. Twelve pins can connect a 6 by 6 array of 36 LEDs or switches. An extra six pins can allow you to have 36 LEDs and 36 switches which is still in the ATmega328 format. There are some constraints; you must be able to wire the switches and LEDs into an array, if you want to press more than two buttons at a time (and know exactly which are pressed) then you need a diode for each button and if you want more than one LED lit at a time there might be brightness constraints but adding a cheap LED driver (MAX7219 module) would allow you to illuminate up to 64 LEDs brightly.

Yes, the coding gets more complex but that is why we are here. :grinning:

Now, please explain the essential purpose of your project and we can provide better advice. :astonished:

Well...I want to build a cryptographer machine. For example, when I press A, I want G to light up but the next time I'm pressing A, I want Q to light up. I want it to change anytime I'm pressing A or other alphabets. For example take NIMA as a plain text. When I press N, F lights up. When I press I, K lights up. When I press M, Q lights up. Finally when I press A, C lights up. But next time that I'm pressing these buttons(N, I, M, A), I want the machine to give me different alphabets. I hope I've made it vividly clear.
Thank you for your useful advice. And something else. Sorry about the way I write...Not that much good in English! :slight_smile:

OK, that explains the 26 keys and LEDs.

So, the rules are, only one key at a time is ever pressed, if more that one is pressed, that would be invalid. And only one LED ever illuminates at a time.

A Nano would perform this task perfectly on its own. You have an array of 6 by 5 keyswitches (not all positions are necessarily used, or actually, you can have four "control" buttons to perform special functions).
And you have an array of LEDs which shares the same 6 columns.
So on the Nano, you have six pins to control the columns, five "row" pins to read the buttons and five "row" pins through 330 Ohm resistors to drive LEDs. Total 16 pins, plenty on the Nano.

You digitalWrite all the columns as LOW to start but have the pinMode as INPUT, the button rows to INPUT_PULLUP and the LED rows as INPUT.

To read the buttons, you set one column at a time as OUTPUT, read the rows in turn looking for one that is LOW because the button at that cross is pressed, then that column back to INPUT and go on to the next.

To illuminate one LED, you set its column only as OUTPUT, its row as OUTPUT and write (only) the row HIGH. To turn it off, you set the column as INPUT, the row as INPUT and write it LOW. If you do not write it LOW again, it would effectively be INPUT_PULLUP and you could get a dim glow on the LEDs.

So your program then consists of spending most of the time with the desired LED illuminated, but periodically (every time millis() increases by 10) switching the LED off, reading all the buttons and deciding on the next action. If there is no change, you go back to illuminating the same LED for another 10 ms.

Do not think that this is complex. It is easy enough to build the code step by step and no less complicated than fiddling with 52 separate pins!



Um...I didn't quit understand. I see 16 switches and 64 leds.
And are these supposed to be wired on a breadboard?
And can I do it with a Uno? Because maybe I want to use the Uno for other purposes later.

When you get diagrams here, you get what you get! :astonished: These are the diagrams I managed to find to explain the concept of a matrix of pushbuttons or LEDs in case you have not encountered it before. For your purpose the matrix in each case is 6 columns and five rows but I am not going to draw that one specially for you. :roll_eyes:

It is up to you to determine how to construct your Enigma. :grinning: That may well depend on how permanent you want this device to be. A UNO is a poor board for making these sorts of things (or in fact, much at all). A Nano with header pins fitted will indeed mount to a breadboard and at about $3 from eBay, you might as well just build the thing and keep it when you get it all working, but your switches and LEDs are gong to be the tricky bit. you know I haven't encountered matrix used in arduino projects and that's why I didn't understand. I tried so many mechanical machines to build this thing but it didn't get anywhere with those simple settings and easy to crack codes. So I decided to go for something electronic in which I was a beginner but at least I had the passion to learn about it. I just wanted to make sure if this project is possible to work on which I understood yes...Damn right it's possible. But as the first person answered my question said, I have to start from the beginning and experience some Aha moments rather than jumping into the main project at first...
If it's possible for you, please explain the matrix of the leds and switches a bit more simpler for me...
Thanks a million! :slight_smile: