Capture the Image Using Ultrasonic Sensor With Arduino




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

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Hardware Explanation

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 receiving time is calculated by the arduino and it will give the input to python. For reference you need more information about ultrasonic visit this link Ultrasonic.

Arduino UNO

Arduino is an open-source prototyping platform based on easy-to-use hardware and software.There are different types of arduino board available as per our requirements we select the boards in this project i am choosing the arduino UNO is a microcontroller board based on the ATmega328 (Datasheet). It has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16 MHz ceramic resonator, a USB connection, a power jack, an ICSP header, and a reset button Arduino UNO.


In this project i am using web camera that capture the image based on the detection.

Step 2: 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);


Step 3: 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 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'

cam.start() 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:
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

Step 4: Run the Program

Declare the arduino port in Python program the above image shows the arduino UNO port connection.

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.



    • Indoor Lighting Contest

      Indoor Lighting Contest
    • Make It Fly Challenge

      Make It Fly Challenge
    • Growing Beyond Earth Maker Contest

      Growing Beyond Earth Maker Contest

    14 Discussions


    Question 1 year ago on Introduction

    is there any other method of image processing without using the camera?
    using sonar sensor.


    4 years ago on Introduction

    is it possible if ultrasonik sensor changes to PIR sensor?

    where can see result capture of image? do you use serial com to connect arduino to PC?

    5 replies

    Reply 4 years ago on Introduction


    I don't try using PIR sensor but it is possible.

    Capture image save in the home/image_captured

    Yes i use serial port for communication


    Reply 2 years ago

    If I understand correctly this can be adjusted to accommodate different trigger distances? I ask because I am looking to create an image capture system for a 3D printer after the completion of every build layer. And I think this might be the starting point for me.


    2 years ago

    Great work! Would you happen to have an example image using this technique that you can share?

    1 reply