Motorcycle Dashcam: Execute code on Arduino when power is lost.

I'm trying to create a motorcycle black box/dashcam. I would like to use an arduino to start a small video camera when a motorcycle is turned on, and recording is stopped after it's turned off. Starting the system when power is turned on is easy, stopping it when power is turned off is the tricky part.

Here's what I was thinking: - small camera (808 keychain) with it's own battery. wires soldered to the power and start/pause button. - usb power to camera to keep onboard battery charged. Usb active only when vehicle is on. - arduino that is hooked up to 2 opto-couplers to simulate button press on power and start/pause buttons. - power to arduino from ???

I was thinking of using a battery on the arduino, but it will die after a short period of time. Also, connecting directly to bikes battery will drain it.

What I'm looking for is a way for the arduino to determine when system power changes from on-off and execute code when that happens. After this code is executed, the arduino should power down until power is applied to the system again.

Here is a simple chart showing when power for the various devices would be on bike power on|---------------|off Arduino power on|---------------------|off Camera Power on |------------|off

What options do I have here that would require zero power to the arduino after the camera 'stop' code is executed? Or is there a better way of doing this (that doesn't require purchasing a dashcam)?

thanks in advance.

You can power the Arduino Vin pin from the motorcycle supply via a diode that feeds a large capacitor between Vin and to ground. The capacitor can power the Arduino for a few tens of milliseconds after power is removed. To detect loss of power, use a voltage divider connected across the capacitor, feeding either an analog input or the analog comparator.

I like that idea, but I need to 'push' the power button for at least 3 seconds. If I used a small rechargeable battery instead of the capacitor, that would give me enough time.

What do you need to do that requires 3 seconds of power?

dc42: What do you need to do that requires 3 seconds of power?

I need to press a button for three seconds and release it to power off the camera.

Is that a 'push to start recording, push to stop recording' type camera? If so, you're in danger of the camera getting out of sync with the Arduino so you end up with lots of footage of parking lots. If there's any other way to control the camera, such as a LANC interface, that would be better.

Assuming you want to get the solution you described working and given that you just want to generate a fixed period pulse, I'd use a 555 timer to generate the timed pulse and just trigger that momentarily from the Arduino. That reduces the time you need the Arduino to keep running after power off to a fraction of a second, which is easily achievable with a capacitor.