Capture Image with camera using Arduino & Ultrasonic

karthikeyan S

1 min read

I have been in IOT space for quite few months and trying to integrate things with Arduino board, Recently I came across Ultrasonic sensor, it is interesting. So I thought of creating a small project.
The project goal is to capture the obstacle for security purpose using ultrasonic sensor with a camera.

Hardware used :

1) Ultrasonic sensor
2) Arduino UNO
3) Camera

Ultrasonic Sensor :

Ultrasonic sensor converts sound wave into electrical signal, they do both transmitting and receiving the signal, It will act like as an transducer. Ultrasonic generates high frequency sound waves so the echo is received back to the sensor in between the transmit time and receiveing time is calculated by the arduino and it will give the input to python.

Connection :

Connect your arduino 12th and 11th pin to sensor Trigger pin and Echo pin, arduino +5v and Gnd pin into ultrasonic positive pin and Gnd pin circuit diagram.
Open arduino ide and paste the arduino code into ide and upload the program into UNO.

Arduino code

Arduino will receive the signal from Ultrasonic and given the signal input to python.
int trigger_pin = 13;
int echo_pin = 11;
float time_taken;
void setup() {
pinMode(trigger_pin, OUTPUT);
pinMode(echo_pin, INPUT);
void loop() {
digitalWrite(trigger_pin, LOW);
digitalWrite(trigger_pin, HIGH);
digitalWrite(trigger_pin, LOW);
time_taken = pulseIn(echo_pin, HIGH);

Python code

Python program is used for getting the input signal from sensor via arduino, so that it can capture the obstacle according to the sensor detection.
#! /usr/bin/env python
import sys
import serial
import pygame
from os import getenv
from pygame.locals import *
from datetime import datetime as dt
# Initializing the Camera device
cam =“/dev/video0″, (640, 480)) // Here we declare the arduino port
home_dir = getenv(‘HOME’)
Adjust the value of this variable to set the distance
for the sensor to detect intruders
RANGE = 300
def capture_image():
Starts the camera, Captures the image, saves it & stops
file_name = home_dir + ‘/image_captured/image_’ + str( + ‘.jpg’
image = cam.get_image(), file_name)
Establishes a connection to Arduino board through serial interface
arduino_board = serial.Serial(sys.argv[1], 9600)
Enters an infite loop that runs until it receives Keyboard Interrupt
while True:
if arduino_board.inWaiting() > 0:
data = arduino_board.readline().strip()
The value received through serial interface would be
string, in order to process futher, it is converted
to numeric datatype.
data = int(float(data))
if data <= RANGE: capture_image() print data except BaseException, be: ''' initially the board might send some strings that are not the numeric value, to handle such exception it is catched and ignored by printing an exception message. ''' print be.message [/source]

Run the Program

For running the program save the python code, open terminal type => python “Your python project name”/arduino port name(example : python /dev/ttys0 ). Arduino port name is shown in arduino ide choose Tools => Port => Port name is shown in ide
Once all these settings are done, When you run the program Ultrasonic sensor will find the obstacles in an interval and capture the images using the camera.
Hope this will give you some idea about using ultrasonic sensor with arduino using Python.

Related posts:

5 Replies to “Capture Image with camera using Arduino & Ultrasonic”

  1. Hello Adrian,This is my firs Arduino and I also want to make exactly the same thing as you did but i have stmeulbd across a serios of problems because some of the code that I am using in my arduino is writen in romanian and I don’t know exactly what to change in my code.Could you please help me ? The thing is that my arduino has Temperature, Humidity, Pressure and light sensor and there is a lot of code I must go through.Kind Regards,Flaviu Vlaicu

  2. Hi Ana. Can you share your code and what error you facing while compiling. So that i can able help you.

Leave a Reply

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