Nintendo Switch: How to Debounce Your NES Controller’s Buttons for Use in Arduino Projects

About: Twitter and Instagram: @barbecue_donuts

If you need to use several switches on your next Arduino Uno project get rid of the pushbuttons and use a classic NES controller to free up 5 digital pins - Build a "Nintendo Switch".

My Instructable will show you how to make a simple circuit to run 8 switches to control 8 LEDs separately using only 3 digital pins for the switches - The 4021 8-bit shift register IC in the controller makes this possible (read more about it in Step 5 of my Nintendo Remote Control Instructable).

This quick project is a continuation of my previous Light Switch Instructable however I have modified the sketch, making the code consistent by referring to all button states as 0s or 1s - 0 = button pressed (LOW) ( as in a usual pushbutton switch with a pull-up resistor ) and 1 = button released (HIGH).

Supplies:

Step 1: Equipment

  • Classic NES controller
  • Arduino Uno
  • Breadboard
  • 8 x LEDs
  • 8 x 330Ω resistors
  • 9 x Jumper wires
  • USB cable
  • Computer with Arduino IDE
  • Power supply

Step 2: Make the Circuit

  • Red wire from Arduino +5V to NES Controller Pin 7 (+5V)
  • Black wire from Arduino GND to NES Controller Pin 1 (GND)
  • Orange wire from Arduino D2 to NES Controller Pin 2 (Clock)
  • Yellow wire from Arduino D3 to NES Controller Pin 3 (Latch)
  • Green wire from Arduino D4 to NES Controller Pin 4 (Data)
  • Black wire from Arduino GND to - Rail Breadboard
  • Red wire from Arduino D5 to Anode of first LED
  • 330Ω Resistor from Cathode of first LED to - Rail
  • Orange wire from Arduino D6 to Anode of second LED
  • 330Ω Resistor from Cathode of second LED to - Rail
  • Yellow wire from Arduino D7 to Anode of third LED
  • 330Ω Resistor from Cathode of third LED to - Rail
  • Green wire from Arduino D8 to Anode of fourth LED
  • 330Ω Resistor from Cathode of fourth LED to - Rail
  • Blue wire from Arduino D9 to Anode of fifth LED
  • 330Ω Resistor from Cathode of fifth LED to - Rail
  • Purple wire from Arduino D10 to Anode of sixth LED
  • 330Ω Resistor from Cathode of sixth LED to - Rail
  • Grey wire from Arduino D11 to Anode of seventh LED
  • 330Ω Resistor from Cathode of seventh LED to - Rail
  • Brown wire from Arduino D12 to Anode of eighth LED
  • 330Ω Resistor from Cathode of eighth LED to - Rail

Step 3: The Sketch

The sketch is below - Feel free to modify for your own needs and save some digital pins for other uses (Please note: if you remove millis(), lastDebounceTime[], and debounceDelay you will get unpredictable results) :

#define NUMBER 8 // define number of buttons and LEDs - In this sketch one button controls each LED.

#define nesClock 2 // NES controller clock pin plugs into D2
#define nesLatch 3 // latch pin plugs into D3
#define nesData 4 // data pin plugs into D4
byte controllerData; //8-bit data received from NES controller by Arduino (e.g. B01111111 when RIGHT BUTTON PRESSED)
byte led[] = {5,6,7,8,9,10,11,12}; // LEDs plug into D5-D12
byte ledState[] = {LOW,LOW,LOW,LOW,LOW,LOW,LOW,LOW}; // LEDs are OFF
byte buttonState[] = {1,1,1,1,1,1,1,1}; // 1 = HIGH - No NES controller buttons currently pressed
byte lastButtonState[] = {1,1,1,1,1,1,1,1}; // 1 = HIGH - No buttons were pressed last time around either
long lastDebounceTime[] = {0,0,0,0,0,0,0,0}; // No buttons pressed last time around so last debounce time = 0 milliseconds
long debounceDelay = 200; //200 millisecond debounce delay works well in this sketch
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // Open serial montior
pinMode (nesLatch, OUTPUT); // LATCH is an OUTPUT pin
pinMode (nesClock, OUTPUT); // CLOCK is an OUTPUT pin
pinMode (nesData, INPUT); // DATA is an INPUT pin
for (byte i = 0; i <8; i++){
pinMode(led[i], OUTPUT); // LEDs are OUTPUT pins
Serial.print("Checking LEDs");
Serial.println(led[i], DEC); // Check D5-D12 and each LED works
digitalWrite(led[i], HIGH);
delay(125);
digitalWrite(led[i], ledState[i]);
}
}
void getControllerData(){ //Extract 8-bit data from NES controller
digitalWrite (nesLatch, LOW);
digitalWrite (nesClock, LOW);
digitalWrite (nesLatch, HIGH); //Trigger latch pin
delayMicroseconds(2);
digitalWrite (nesLatch, LOW);
controllerData = digitalRead (nesData);
  for (int i = 1; i <=7; i ++){
digitalWrite(nesClock, HIGH); // Pulse clock pin
delayMicroseconds(2);
controllerData = controllerData <<1; // and bit-shift
controllerData = controllerData + digitalRead (nesData);
delayMicroseconds (4);
digitalWrite (nesClock, LOW);
}
}
void loop() {
// put your main code here, to run repeatedly:
for (int i = 0; i < NUMBER; i++){
buttonState[i] = bitRead (controllerData, i); //Look for at state of each button in order
getControllerData(); //Get 8-bit data from controller (e.g. if B01111111 received from control // ler RIGHT button pressed
// e.g. If RIGHT button pressed and last time round RIGHT button was pressed for long enough:
if (buttonState[i] == 0 && lastButtonState[i] == 0 && millis() - lastDebounceTime[i] > debounceDelay){
if(ledState[i] == HIGH){ //e.g. If LED plugged into D5 is ON
ledState[i] = LOW; // Turn it off
Serial.print (led[i], DEC); // See it happen on serial monitor
Serial.println (" is OFF");
}
else{
ledState[i] = HIGH; //e.g. If LED plugged into D5 is OFF
Serial.print (led[i], DEC); // Turn it on
Serial.println (" is ON");
}
lastDebounceTime[i] = millis();
}
digitalWrite (led[i], ledState[i]);
lastButtonState[i] = buttonState[i];
}
}
Arduino Contest 2019

This is an entry in the
Arduino Contest 2019

Share

    Recommendations

    • Woodworking Contest

      Woodworking Contest
    • Gardening Contest

      Gardening Contest
    • IoT Challenge

      IoT Challenge

    2 Discussions

    0
    None
    conissocool

    10 days ago

    you need to just sell those online I would buy all of them

    1 reply
    0
    None
    barbecue_donutsconissocool

    Reply 8 hours ago

    Thanks. NES controllers are readily available at pawn brokers for about $AU 9 here in Australia. They’re pretty easy to plug wires into and experiment with and to make the projects yourself.