Nintendo Switch Control Library - Create controller macros using Arduino Leonardo

TheStonedModder

Well-Known Member
Member
Joined
Dec 25, 2022
Messages
2,625
Reaction score
3,744
Trophies
2
Age
29
XP
8,069
Country
United States
Please note, I am not the creator or maintainer of this Library. All credit goes to @lefmarna I've only recently learned about this, and wanted to contribute to his project by creating some documentation/resources in English.


For this guide you will need the following:

An Arduino Leonardo (I use this one)
Nintendo Switch Control Library (found in Arduino IDE)

This guide will be done using windows, if you would like to learn how to use OSX I recommend checking out this blog post from the creator of the Library we are using @lefmarna


Install Legacy Arduino IDE 1.8.x(Download) Do not install Arduino 2.x.x! as it currently does not function properly with this library.



Ensure that Arduino AVR 1.8.3 is installed under your boards manager. If you have a newer version installed. Please use the "select version" box to install 1.8.3. Anything newer will not function properly




Now it is time to edit your boards.txt(Make sure to close out of your IDE first, or your changes might not be detected right away) for me on windows this is located within my IDE's installation directory.
Code:
C:\Program Files (x86)\Arduino\hardware\arduino\avr\boards.txt
Once you have boards.txt open in an editor of your choice(I use sublime text) You will need to scroll down and edit lines 285, 286, 311, & 312 to match the following
Code:
Line 285: leonardo.vid.1 =0x0f0d
Line 286: leonardo.pid.1 =0x0092

Line 311: leonardo.build.vid=0x0f0d
Line 312: leonardo.build.pid=0x0092

Now the final step is to open Arduino IDE once more and install the Library by selecting "Library Manager" from the "Tools" menu within your IDE

From here you can start creating a simple macro/bot by flashing some basic code like the following to your Arduino, and after plugging it directly into your Switch or Dock

Code:
#include <NintendoSwitchControlLibrary.h>
char userInput;
void setup()
{
    Serial1.begin(9600); // sets up serial
    pinMode(LED_BUILTIN, OUTPUT);
    pushButton(Button::B, 500, 5); //turns on the controller by pressing B(any button works)
}


void setup(){
 
    pushButton(Button::B, 500, 5);
}


void loop(){
    pushButton(Button::X, 600);
    pushHat(Hat::RIGHT, 200);
    pushHat(Hat::DOWN, 200);
    pushHat(Hat::LEFT, 200);
    pushHat(Hat::UP, 200);
    pushButton(Button::X, 600);
}


I'm using this project to learn how to use Python and use Arduino's so please assume there is a much better way of doing this than I am. That being said, if you do know of ways this process can be improved or even just done better. Please let me know, as I would love to keep learning!

You can use any USB to TTL converter for this guide. However you will need to adjust your wiring accordingly. For this guide I will be using this converter
Connect your USB to TTL converter to your Arduino Leonardo. Like so
Arduino LeonardoUSB to TTL
RXWhite Wire
TXGreen Wire
GNDBlack Wire
Now you will want to flash some basic code to your arduino. This code will allow you to interact with your arduino using the python script and serial1(this is your USB adapter. Normal serial is the arduino itself)

Code:
#include <NintendoSwitchControlLibrary.h>

char userInput;

void loop(){
{
    if (Serial.available() > 0) {
        int delayTime = 0;
        int numBlinks = 0;

        userInput = Serial.read();

        if(userInput == 'w'){
            digitalWrite(LED_BUILTIN, HIGH);
            pushButton(Button::HOME);
        }

        if(userInput == 'e'){
            digitalWrite(LED_BUILTIN, HIGH);
            pushButton(Button::B);
        }

        if(userInput == 'r'){
            digitalWrite(LED_BUILTIN, HIGH);
            pushButton(Button::X);
        }

        if(userInput == 't'){
            digitalWrite(LED_BUILTIN, HIGH);
            pushButton(Button::Y);
        }
}


This is an example python script. It will create a GUI with 4 buttons that will send an input to your switch each time you click one
Code:
import serial
import tkinter as tk
from tkinter import *
from tkinter import messagebox
import time

##Open and connects to the configured serial port
comPort = 'COM12'
ser = serial.Serial(comPort, baudrate = 9600, timeout = 1) #timeout stops the connection process after x seconds

#commands
def pressA():
    ser.write(b'w')
    print('A pressed!')
  
def pressB():
    ser.write(b'e')
    print('B Pressed!')
  
def pressX():
    ser.write(b'r')
    print('X Pressed!')
  
def pressY():
    ser.write(b't')
    print('Y Pressed!')
      
  
def menuBlinkEnable():
    if blinkState.get() != 1:
        blinkState.set(1)
    blinkLED()
  
def menuDelaySelect(index):
    if blinkState.get() == 0:
        blinkState.set(1)
    userDelay.set(blinkTime[index])

def menuTurnOn():
    if blinkState.get() == 1:
        blinkState.set(0)
        ser.write(b,'0')

def menuTurnOff():
    if blinkState.get() == 1:
        blinkState.set(0)
        ser.write(b,'x')
      
def menuSave():
    print('Selected Save')
  
def exitGUI():
    root.destroy()

#init window
root =Tk()
root.title('GBATemp Example;)

#define and place buttons (creates and places widgets)
btn_ON= tk.Button(root, text='Press A', command=pressA)
btn_ON.grid(row=0, column=0)

btn_Off = tk.Button(root, text="Press B", command=pressB)
btn_Off.grid(row=0, column=1)

btn_Off = tk.Button(root, text="Press X", command=pressX)
btn_Off.grid(row=2, column=0)

btn_Off = tk.Button(root, text="Press Y", command=pressY)
btn_Off.grid(row=2, column=1)

#menu bar class
menuBar = Menu(root)

#creates and assigns the file menu
fileMenu = Menu(menuBar,tearoff=0)

#Creates and assigns the file menu options
menuBar.add_cascade(label ='File', menu = fileMenu)



#creates and assigns the settings menu
settings = Menu(menuBar, tearoff = 0)

#creates and assigns the settings menu options
menuBar.add_cascade(label='Settings', menu = settings)

##GUI Size Settings
root.config(menu = menuBar)
root.geometry("350x350") #size of the window
root.mainloop() #makes sure the GUI stays running




More guides will be added to this thread as I learn more & if anyone has any feedback on anything I can do, to make this post more clear, informative, or even more efficient in terms of the code posted. Please don't hesitate to let me know, as I would love to keep learning!




I’m not sure why this post keeps adding list BBCODE to the end of it randomly. If the posts formatting seems broken to you, please refresh since I’m actively adding to this guide and trying to fix it (everytime I edit it, those lists get added and break it)
 
Last edited by TheStonedModder,
Thank you, I have been banging my head against a wall all day to get this working :rofl: but this guide was easy to follow and exactly what I needed.
Yea it took me a few days to figure out myself as well, so I figured there must be at least one other person having similar headaches I’m happy to hear you got it working though!

What are you trying to make?
 
Tomorrow…

9B73D558-F655-49E5-BB57-411B061344AB.jpeg

Not sure what I want to use it on but something will pop up I’m sure.

Do people do things like speed runs using this type of tool? I think it would be a cool ”analog” way to make a 100% game save of something.
 
  • Like
Reactions: TheStonedModder
Tomorrow…

View attachment 344536

Not sure what I want to use it on but something will pop up I’m sure.

Do people do things like speed runs using this type of tool? I think it would be a cool ”analog” way to make a 100% game save of something.
I don’t believe anyone has done any speedrunning with these tools yet. But that would be super cool to see.

I’ve mostly only seen it used for like Pokémon or Xenoblade tasks personally

I want to eventually be able to control it all via Python. So I can try and script glitches, and stuff. All in this case being things like button timings (how long they are pressed for) instead of having to configure that bit within the arduino IDE itself.
 
  • Like
Reactions: binkinator
Yea it took me a few days to figure out myself as well, so I figured there must be at least one other person having similar headaches I’m happy to hear you got it working though!

What are you trying to make?
I am just making some simple Mario Kart 8 Deluxe automations for now. I am still thinking of ideas. There are definitely many games and activities you can automate.
 
  • Like
Reactions: TheStonedModder
I am just making some simple Mario Kart 8 Deluxe automations for now. I am still thinking of ideas. There are definitely many games and activities you can automate.
Nice! I eventually want to try and do the same, but all from python so that I(we?) don't need to keep flashing the arduino and swapping cables everytime we want to test an automation

I'm most of the way there, but I can't seem to figure out how to have configurable timings for each specific button, from a text box on the python side yet.

I.e. a box with "500" as the int that the arduino can read and say "Hold A for 500ms" if that makes sense

This is the basic test program I have, that I plan on expanding/sharing once this bit is figured out
 

Attachments

  • fTrP8rn.png
    fTrP8rn.png
    182.7 KB · Views: 244
Well, well, well…looks like others have had similar thoughts about automating a run through.

https://learn.sparkfun.com/tutorials/nintendo-switch-macro-recording-on-the-raspberry-pi/all

This method is a bit more expensive to be sure, but they have a recording feature and automated play of Mario’s 1-1 world. I wonder how the places that track speed runs prevent these types of tools being used. They must require video of your hands actually playing or something. 😂
 
  • Like
Reactions: TheStonedModder
How feasible would it be to Connect my Leonardo to my switch and have my pro controller connected to the Leonardo to just have macros activated from my pro controller?
 
How feasible would it be to Connect my Leonardo to my switch and have my pro controller connected to the Leonardo to just have macros activated from my pro controller?
A macro for your macros you say? Check out the link @TheStonedModder put up. You would have your PC nearby with 104 macro keys on tap.
 
How feasible would it be to Connect my Leonardo to my switch and have my pro controller connected to the Leonardo to just have macros activated from my pro controller?
I dont know the most about programming with the Arduino Im learning everything as I go

But the easiest/best way to accomplish what you want I think would be to. Use the site I linked above to create all the different macros you want and link back to them in your IDE so that the script is run whenever you press a button (or I think you could even wire up an esp32 back to the leonardo so you can run them from a webpage on your phone)

Ive been making my own "switch up game companion" like collective minds sells. With a similar process
 
  • Like
Reactions: binkinator
Are you still working with these? I've been using this.

8bitdo-ultimate-wireless-controller-switch-windows

and trying to convert from
phantom-hand

I've had a really hard issue getting this movement (slingshot)

With a macro/paddle.



Have you tried much like these hard coded controller macros?
 
I'm having trouble getting the nintendo switch to recognize my arduino leonardo as a controller. Any ideas?
I am too, I have changed the board code and am using Arduino 1.8.3 and once I connect it to the switch it does nothing.
 
That means you didn’t edit your boards.txt file properly or first before flashing the arduino
I did all the steps properly and my ide is 1.8.13 and it works completely fine on my laptop but not when connected to switch
Post automatically merged:

I did all the steps properly and my ide is 1.8.13 and it works completely fine on my laptop but not when connected to switch
I got it. My avr board version was 1.8.6 and not 1.8.3
 
Last edited by Lavish_Singal,
he I'm on 1.8.19 and I just checked my boards.txt file and I edited the right stuff I'm also on the right avr board version. pls someone HELP meeeee! btw I have like 0 experience with Arduino so like idk it's probably something stupid.
 
he I'm on 1.8.19 and I just checked my boards.txt file and I edited the right stuff I'm also on the right avr board version. pls someone HELP meeeee! btw I have like 0 experience with Arduino so like idk it's probably something stupid.
What part do you need help with? What errors do you have?
 
it seems to be sending info when I test it on my computer (based on the RX and TX lights on my board) but when I plug it into my switch it stops sending and I'm pretty sure the USB port and the cable I'm using are working.
 
Last edited by Tataloupp,

Site & Scene News

Popular threads in this forum