Is it possible to use an ultrasonic sensor to wake up an Arduino which is in deep sleep when an object is detected within a few cm and then do stuff?
For a truly low-power, Arduino-based solution at least the following is needed:
A) HARDWARE SIDE:
- Use a supply voltage and clock frequency as low as possible. The Arduino Pro Mini 3.3V @ 8Mhz board is a good starting point for a first project.
- Get rid of all the LEDs in the board. Their consumption is in the mA range, way too high.
- Get rid of the onboard regulator and replace it with a low quiescent current, low dropout regulator like the MCP1700. It can source only 250 mA whereas the AMS1117 is rated up to 1A, but 250 mA will probably be good enough for your low-power application. OTOH, just in case you were wondering: you don't need a switching regulator unless your Arduino spends most of the time awake and consuming a lot of current, which is a very unlikely scenario. The high quiescent current of switching regulators rules them out for this kind of applications.
- Select a low-power consumption sensor and signal conditioning circuitry. PIR and CMOS logic family 74HC are good technologies for this. As pointed by Nick Alexeev, an ultrasonic sensor will be constantly emitting, thus rendering useless your power savings on the Arduino. Avoid it.
- The signal conditioning circuit must generate a level interrupt from whichever signal is delivered by the sensor. Timing elements (like capacitors) or latches may be required for this. To be on the safe side, design it for a 100 ms resettable pulse.
B) SOFTWARE SIDE:
- Install a low power Arduino library like this one. Use the ATmega328p power-down sleep mode.
- Write a interrupt service routine (ISR). This is the piece of code that will deal with whichever needs to be done upon an interrupt event.
- Configure (attach) the interrupts as required. Note that only level-triggered interrupts can wake up ATmega328p from power-down sleep mode, as per its datasheet:
If you really want to minimize power, you need to not only have a low-power MCU circuit, but an ultrasonic receiver which you can disable.
Wake up the MCU from a low power periodic timer and fire a pulse, then go back to sleep for at least the sensor ringing time, or the time of the minimum range of interest.
Wake the MCU and the receiver up again at the point in time where you first expect an interesting echo, and stay awake only until you see one or for the range of time at which your echo could arrive.
Take action if you hear an echo.
Once you hear an echo or the window of interest expires, turn everything but the low power timer off again until the next measurement interval.
How frequently you need to repeat the measurement depends on how quickly your objects can be moving and/or how short an actuation of the output is meaningful and/or the maximum permitted latency in responding to one appearing or disappearing. As a lower bound, you want to make sure not to take a new measurement until any multi-bounce or distant-wall reflections of the old transmit pulse have died out.