Using a pair of Arduinos to mimic a keyboard

Here’s a video:

So in my last post I showed you a way that I used a single arduino to talk to a Raspberry Pi (or any other computer) over HID. I’ve updated the project a bit and now I can input any number of things into the pi. Basically this is how it works.

Serial Console on computer -> Arduino Mega -> software serial port -> Arduino micro -> HID on other second computer (in this case the Pi)

There are two buttons that handle pressing the enter key and the delete key as those are hard to send over serial.

There are a few bits of relevant code, both for the arduino. Here’s the mega’s code:

int sent_led = 44;
int test_button = 30;

byte inread;

#include <SoftwareSerial.h>
SoftwareSerial SoftSerial(34, 36); // RX, TX

void setup(){
  pinMode(sent_led, OUTPUT);
  pinMode(test_button, INPUT);

  Serial.begin(9600);
  SoftSerial.begin(4800);

  SoftSerial.println("Hello, world?");

}

void loop(){
  if (Serial.available()>0){
    digitalWrite(sent_led, HIGH);
    Serial.print("Sending: ");
    inread = Serial.read();
    Serial.write(inread);
    SoftSerial.write(inread);
    Serial.print(" , ");
    Serial.print(inread);
    Serial.println("");
    digitalWrite(sent_led, LOW);
  }

}

And here’s the side for the arduino micro, that writes as an HID.

int stat_led = 3;
int enter_led = 4;
int delete_led = 5;

int enter_button = 6;
int delete_button = 7;
int engage_switch = 8;

#include <SoftwareSerial.h>
SoftwareSerial SoftSerial(11, 10); // RX, TX

void setup(){
pinMode(enter_led, OUTPUT);
pinMode(delete_led, OUTPUT);
pinMode(stat_led, OUTPUT);

pinMode(enter_button, INPUT);
pinMode(delete_button, INPUT);
pinMode(engage_switch, INPUT);

Serial.begin(9600);
SoftSerial.begin(4800);

Keyboard.begin(); //

}

void loop(){
if (SoftSerial.available()){
digitalWrite(stat_led, HIGH);
Keyboard.write(SoftSerial.read());
digitalWrite(stat_led, LOW);
}

if (digitalRead(enter_button) == HIGH){
Keyboard.press(KEY_RETURN);
delay(100);
Keyboard.release(KEY_RETURN);
digitalWrite(enter_led, HIGH);
}
else if (digitalRead(enter_button) == LOW){
digitalWrite(enter_led, LOW);
}

if (digitalRead(delete_button) == HIGH){
digitalWrite(delete_led, HIGH);
Keyboard.press(KEY_BACKSPACE);
delay(100);
Keyboard.release(KEY_BACKSPACE);
}
else if (digitalRead(delete_button) == LOW){
digitalWrite(delete_led, LOW);
}

}

3 Comments

      1. I find this brilliantly. First i am just starting micro, with a keyboard project. This gives me a place to start of. Second i like the way you solve the problem with what on hand 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.