Microcontroller projects

NUM or CAPS lock indicator

last updated: 2022-12-23

Introduction

This will be a short project :)

My new keyboard has no NUM lock LED. I have still a bunch unused Teensy 2.0. So let's use one to indicate if NUM or CAPS lock are activated.

The hardware

We need a Teensy 2.0.

num<em>lock</em>teensy

The software

Include the keyboard library

After installing Teensyduino (Arduino 1.8.19) we can use the Tools menu to add the keyboard libs by choosing the right USB Type:

keyboard lib ard ide

In Arduino IDE 2.x we need to add the following JSON String to File > Preferences... to add Teensyduino:

    https://www.pjrc.com/teensy/package_teensy_index.json

Tweak the lib

We will use the variable keyboard_leds which gives the following outputs:

keyboard_leds CAPS lock NUM lock
0 off off
1 off on
2 on off
3 on on

The variable is unfortunately private and to get the program run we need to add

    #include "usb_private.h"

to the file hardware/teensy/avr/cores/usb_serial_hid/usb_api.h.

In Linux you find it under home/myusername/.arduino15/packages/teensy/.

The code

Now we can programm the following sketch an the Teensy LED will indicate if NUM lock is on or off :). Naturally a similar program could indicate the state of CAPS lock.

    /*
      teensy2_num_lock_indicator.ino
      NUM lock indicator using Teensy 2.0 built in LED.  
      Coose an USB Type with "keyboard" in Tools menu of 
      Arduino IDE before programming.
      weigu.lu
    */

    //#define DEBUG

    const byte PIN_TEENSY2_LED = 11;

    void setup() {
      pinMode(PIN_TEENSY2_LED, OUTPUT);  
      #ifdef DEBUG
        Serial.begin(115200);
        delay(1000);
        Serial.println("keyboard"); 
      #endif // #ifdef DEBUG
    }

    void loop() {
      if (keyboard_leds==1) {
        digitalWrite(PIN_TEENSY2_LED,HIGH);
      }
      else {
        digitalWrite(PIN_TEENSY2_LED,LOW);
      }
      #ifdef DEBUG
        Serial.println(keyboard_leds);
      #endif // #ifdef DEBUG
      delay(10);
    }

Downloads