Control 10 output pins using 10 push button switch with 1 Arduino input pin ANPB-V2

Project Video
Project Details

In this article, we learn how to connect up to 10 push buttons to one Arduino input pin by controlling relays to turn ON/OFF or toggle AC bulb. This way we save Arduins pins for
other purposes.
For each push button we need one resistor. For example if you want to use 5 push buttons, then we need 5 resistors and one ground resistor.
Ground resistor should be 50 to 100 times largers than resistors connected to push buttons.

Selecting Resistor for this purpose

In this video I've shown to use 1kΩ resistors
and for ground resistors I've selected 100kΩ resistor. We can use carbon film resistors with 10% tolerance or metal film resistors
with 1% tolerance. The lower the tolerance number the better the result which allows using more push buttons. For example 1% tolrant resistors are better than 10% tolrant resistors.

Project Images
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image

Project Code


/*
 * this is Version 2: V2 Which has relay toggle options
 * This is Arduino code to use 2 to 10 push button switches with Arduino UNO, Mega, Nano wither with 3.3V or 5V
 * 
 * V1 Video is just to control output and do whatever you want with it: https://youtu.be/MeWgnt0YLj8
 
 * Written by Ahmad Shamshiri for Robojax Robojax.com
 * on Nov 27 to Dec 04, 2020  in Ajax, Ontario, Canada
 Watch the video instruction for this sketc (V2) h: https://youtu.be/AI-zzmSZVfw
 

If you found this tutorial helpful, please support me so I can continue creating 
content like this. You can support me on Patreon http://robojax.com/L/?id=63
or make donation using PayPal http://robojax.com/L/?id=64

* 
 * Code is available at http://robojax.com/learn/arduino

 * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see . 

*/

unsigned int pushbuttonsCount=10;//number of push buttons and resistors
unsigned long resitorValue=1000;
unsigned long resitorToGround=100000;

byte inPin =A0;
float sourceVoltage=5;//5V or 3.3V depending on your arduino
char *push_button_name[]={"PB1", "PB2", "PB3", "PB4","PB5","PB6","PB7", "PB8", "PB9", "PB10"};
bool pbValue[]={0,0,0,0,0,0,0,0,0,0};//this holds the status of each push button
unsigned int outputPin[]={2,3,4,5,6,7,8,9,10,11};


int pushDelayTime=100;//100 milliseconds watch video https://youtu.be/MeWgnt0YLj8 for details
float V_tolerance =0.05;//10% (for example) for 1% set 0.01 for 5% set 0.05
bool debug=true;//to display votlage and calculations
///https://www.arduino.cc/reference/en/language/functions/analog-io/analogread/ 
//https://www.arduino.cc/reference/en/language/functions/analog-io/analogreference/
const byte BIT_SOLUTION=10;//for Due and MKR set to 12.

byte outputON=0;//for output control
byte outputOFF=1;
bool toggleOutput=false;

void readPushButtons();
void setup()
{
// analogReadResolution(BIT_SOLUTION);//for Due, MKR only
 Serial.begin(9600);
 Serial.println("Robojax.com Code");
 Serial.print(pushbuttonsCount);
 Serial.println(" Push buttons");
 for(int i=0; i= ( voltage_minimum)
         )
         {
          if(toggleOutput)
          {
            pbValue[i]=1-pbValue[i];//set puths button value to true
            delay(pushDelayTime);//push time limit
          }else{
             pbValue[i]=true;//set puths button value to true
            delay(pushDelayTime);//push time limit           
          }
        }else{
          if(!toggleOutput)
          {
            pbValue[i]=false;
          }
        }//if
    }//for 
    
   Serial.println();
}//readPushButtons()


/*
 * controlOutput(int i, int status)
 * @brief contorls the output pin
 * @param i is the corresponding to the push button pressed starting with zero 
 * @param status is either 1 or 0. 1=on, 0=off
 * @return none
 * Written by Ahmad Shamshiri for Robojax.com 
 * on Dec 04, 2020 in Ajax, Ontatio, Canada
 * 
 */
void controlOutput(int i, byte status)
{

  digitalWrite(outputPin[i], status);
    if(debug && status){
  Serial.print("\nPin ");
  Serial.print(outputPin[i]);
  Serial.println(" is ON");
    }else{
  Serial.print("\nPin ");
  Serial.print(outputPin[i]);
  Serial.println(" is OFF");      
    }
}//controlOutput(int i) end

Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image