0

I'm looking for two answer here.

  1. Is it possible to power a Leonardo to be always on, separate from the power from the USB host - I've tried with a full size Leonardo and a power adapter but for some reason the code didn't run when I did that. I've bought a battery shield too which will be arriving soon.

  2. I want it be power-able via a switch with batteries so that I do a few things. For a start avoid it's boot up time and have it pressing a key as soon as the booted computer sees it (I'm running it as a keyboard/mouse via USB-HID) and also at one point I have the computer send serial data to have the Arduino run a different part of it's code...at this point the host computer reboots. Obviously that means the Arduino will see it's USB host disappear then come back online, will that automatically reset it and thus start it from the beginning again, or can I set it so that with always on power it will carry on doing what it is, irregardless of it's connecting to the host?

Do I need to change code, or libraries to achieve any of this?

With the theory intact answered, here is some of my actual code, i've stripped most of it out as it's just a bunch of keyboard presses

#include <Keyboard.h>
#include <Mouse.h>
char returnKey = KEY_RETURN;
char tabKey = KEY_TAB;
void setup() {
Keyboard.begin();
Mouse.begin();
// delay(60000); //Deploy Delay
delay(3000); //Testing Delay
// Keyboard commands
Keyboard.write(returnKey);
delay(14000);
// Welcome Screen 
Mouse.click(MOUSE_LEFT);
delay(100);
}

void loop() {
}
8
  • Please show us the code and tell us which libraries you use and where they come from (only for libraries that are not standard included with arduino). Beside the arduino keyboard there are other ways to implement a usb keyboard: github.com/NicoHood/HID/wiki/Keyboard-API
    – Jot
    Jan 7, 2019 at 12:25
  • Have added an example of my code Jan 7, 2019 at 13:10
  • When do you expect those keyboard and mouse commands to execute?
    – Majenko
    Jan 7, 2019 at 13:22
  • @Majenko, at the moment 3 seconds after it's connected to the computer. Or instantly ideally. I plug the power barrel in and the ON and L leds light up - but nothing happens when I plug the USB in. I have to remove the power barrel and then it appears to reconnect to the USB and begin operating. It's as if the power barrel is stopping the USB port from doing anything on my device. Jan 7, 2019 at 13:28
  • Well, they run 3 seconds after the board is powered up. Regardless of what the USB is doing. I am not aware of any way of saying "Run this once the USB is connected" in the current API.
    – Majenko
    Jan 7, 2019 at 13:30

2 Answers 2

1

Is it possible to power a Leonardo to be always on, separate from the power from the USB host

Yes, that is what the barrel jack is for.

I've tried with a full size Leonardo and a power adapter but for some reason the code didn't run when I did that.

Then your code is wrong. Are you using while (!Serial); or something similar near the top of your code?

For a start avoid it's boot up time

The most certain way of doing that is to not use the bootloader. Instead program the board directly with a hardware programmer disabling (or erasing) the bootloader.

Obviously that means the Arduino will see it's USB host disappear then come back online, will that automatically reset it and thus start it from the beginning again, or can I set it so that with always on power it will carry on doing what it is, irregardless of it's connecting to the host?

The Leonardo only reboots if you open the serial port at 1200 baud, or toggle the RESET pin / button.

Do I need to change code, or libraries to achieve any of this?

It sounds like you may need to, yes - since it's not working at the moment when if the code were correct it should work.


IMPORTANT

Most computers will not recognize the Leonardo until the operating system is fully booted. This means you cannot use the Leonardo to interact with the BIOS or boot menu. The Leonardo does not implement the "Boot Protocol" keyboard which is required to interact with the BIOS or other pre-boot environment.

2
  • Thanks Majenko - it looks like I can do what I want in theory then. No "while (!serial)" at the top now, but as soon as I used the barrell connector on my full size Leonardo the code stopped running - it powered on and then it just seemed to ignore the USB connection. Fortunately in my testing the macOS works with the default keyboard and with the BootKeyboard available in the HID-Project library, so I'm covered there too fortunately. Jan 7, 2019 at 12:59
  • Have added an example of my code Jan 7, 2019 at 13:10
0

It is possible to tell if the computer has opened or closed the serial port.

The serial port is located on the Arduino board.
By testing if(Serial) you can check if the serial port has been made active by the computer.
You could send keyboard and mouse events after it became active.

The fading in and out of the led at pin 13 is done by the bootloader. You could upload the sketch with a programmer to run without bootloader.

As far as I know, even without bootloader the serial port is still created.
The Teensy boards have a little more control over the USB interface.

To know what is going on and when a computer is connected, a mini linux computer is a lot easier. For example a Raspberry Pi.
The ATmega32u4 of the Arduino Leonardo has the possibilities and the information is available, but as far as I know, there are no libraries functions to use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.