Toggle two led with one button

hi all, i want help to write a small arduino code for toggle two led with one button. i have a ezbuton library code but it about 803 bytes and i want a code about 400 bytes.

Welcome to the community - although this is not an Arduino forum :wink:

If you want the smallest possible code, you may want to avoid using libraries as they usually are supposed to package a wealth of functionality in easy to use commands, which will inevitably mean accepting some overhead.

Also for your request you haven’t really laid out all your requirements.
Toggling two LEDs with one buttons can mean several things.

  • both LEDs do the same ON/OFF
  • both LEDs oppose eachother
  • two LEDs act like a binary counter ( 00 → 01 → 10 → 11 → rinse repeat)

just to name a few :wink:

BTW, do you mean 400 byte binary or 400 characters written code?


hello ScruffR, thanks to your reply. i want * both LEDs oppose eachother, 400 byte code file size.

Although I have no Arduino to test this with this would be a way I could imagine doing it :wink:

const uint32_t usDEBOUNCE = 500; // 500µs debounce periode 
const int pinLED[] = { 0, 1 };
const int pinButton = 2;
const int numLEDs = sizeof(pinLED) / sizeof(pinLED[0]);

volatile bool state = false;

void setup() {
  for (int l=0; l < numLEDs; l++) 
    pinMode(pinLED[l], OUTPUT);

  pinMode(pinButton, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(pinButton), toggle, RISING);

void loop() {
  static bool prevState = true;
  if (state != prevState) {  // only act when something changes
    prevState = state;
    digitalWrite(pinLED[0], state);
    digitalWrite(pinLED[1], !state);

void toggle() {
  static uint32_t usLast = 0;
  if (micros() - usLast < usDEBOUNCE) return; // simple button debounce
  usLast = micros();
  state = !state;

To reduce code length (not binary size) you could use shorter variable names and remove the comments :wink:

thanks, but this code use 785 bytes

Have you checked your base line?
How big would this build in your environment?

void setup() {}
void loop() {}

May I ask why you are that space constrained?
Which exact board are you using?

If you were using a vanilla Atmega based Arduino you could get away with some dirty programming like relying on the default pinMode().
Without all the “fanciness” of good style you could do this

void setup() {
  //pinMode(2, INPUT_PULLUP); // default mode is INPUT -> external pull-up required
  pinMode(3, OUTPUT);   
  pinMode(4, OUTPUT);

void loop() {
  static bool state = false;
  if (!digitalRead(2)) {
    state = !state; 
    digitalWrite(3, state);
    digitalWrite(4, !state);

This will require an external pull-up resistor for the button, does not account for button bounce, will initially not light up any LED.
If you want your binary any smaller than this, I guess you’d need to add even more external hardware and/or resort to bare metal and machine code programming - which is definitely not in the scope of this forum.