Take picture programmatically

Is there a way to take pictures programmatically (for a timelapse, or from a movement detection sensor) ?