The distance measurement tool

The distance measurement tool

What I have made here is a distance measurement tool. I have put together some parts I had laying around, like an LCD and the DYP-ME007 ultrasonic distance sensor. The idea of this project is really just to familiarize myself with interfacing LCDs using the AVR LCD library from Peter Fleury, which turned out to be very easy, so I can absolutely recommend this library!

I wanted a bar-graph and plain text to visualize the distance to a object that I will point to. I also made a “tare” functionality so you can set a predefined distance to add or subtract other distances based on the tare distance. If the word “tare” is right for this application I don’t know. It is what’s printed on a button on my kitchen weight that does the same thing. Below I will post some pictures and source code for you to look at, but this time I also wanted to videotape the process of making this. So here it goes

Parts list:
AtTiny2313
DYP-ME007 Ultrasonic Distance Module
HD44780 16×2 LCD
2 tactile switches
Some generic components and a stripboard

The code (AVR GCC Compiler)

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include "lcd.h"

volatile int count = 0;
volatile int valueint;
volatile int tareint=0;
char tarestring[7];
char valuestring[10];

ISR(INT0_vect)
{
	tareint=valueint;
}

ISR(TIMER1_CAPT_vect)
{
	valueint=(ICR1-100)/5;
	itoa(valueint-tareint, valuestring, 10);
	TCNT1 = 0;
	//is triggered each time echo pin goes low.
	//Read out tcnt value
}

ISR(TIMER1_COMPA_vect)
{
	//is triggered every 6th ms
	PORTD ^= (1<<PIN5);
	_delay_us(20);
	PORTD ^= (1<<PIN5);
	//TCNT1=0;
	count++;
	
}

void updatebar(void)
{
	int i;
	cli();
	lcd_home();
	lcd_puts("                ");
	lcd_home();
	for(i=0 ; i<(valueint/10) ; i++)
	{
		lcd_putc(255);
	}
	sei();	
}

void updatenumber(void)
{
	cli();
	lcd_gotoxy(0,1);
	lcd_puts("                ");
	lcd_gotoxy(0,1);
	lcd_puts(valuestring);
	lcd_gotoxy(4,1);
	lcd_puts("cm");
	lcd_gotoxy(8,1);
	lcd_puts("Tare=");
	lcd_gotoxy(13,1);
	itoa(tareint,tarestring, 10);
	lcd_puts(tarestring);
	sei();
	count=0;
}


int main(void) 
{    
	DDRD=0b00110000;
	PORTD |= (1<<PIN2);

	//INT0 init
	MCUCR |= (1<<ISC01); //falling edge
	GIMSK |= (1<<INT0); //enable
	
	//TIMER1 INIT
	TCCR1B |= (1<<ICNC1) | (1<<CS10) | (1<<CS11) | (1<<WGM12);
	TIMSK |= (1<<ICIE1) | (1<<OCIE1A); //Interrupt on ICP1 & OCA
	OCR1A = 8000; 
	
	
	//LCD INIT
	lcd_init(LCD_DISP_ON);   /* initialize display, cursor off */ 
	lcd_puts("my shiny new lcd");
	lcd_gotoxy(3,1);
	lcd_puts("is WORKING!");
	sei();
	while (1)               /* loop forever */ 
	{
		//PORTD |= (1<<PIN4);
		if(count>=10)
		{
			updatebar();
			updatenumber();
		}
	}    
}

This article has 13 comments

  1. could you post a quick schematic please

  2. Hey Henrik – how do I make a wireless rangefinder as broadcasting every hour with a distance and to work with batteries (Low power)
    I thought maybe I could use HC-SR04, ATTiny2313 and NRF24L01 + but I can not assembly code – only Visual Studio. NET (C #, VB, C + +) but can make the receiver portion with Arduino and NRF24L01 + or FEZ Panda II – but need help to the transmitter part HC-SR04, ATTiny2313, NRF24L01 + and low power … Sincerely Paw

    • Henrik Sandaker Palm
      Friday 20 April 2012, 10:54 am

      If you can make the receiver portion with an Arduino you should be able to make the transmitter portion with an arduino also. You can pull the atmega out of the arduino board after programming and make your own minimalistic PCB, so it will use less power (using low voltage dropout regulators or running directly off of a battery and remove LEDs etc.). Also remember to use low power modes like sleep function.

      • Hey, Just a quick question. How would you chngae the delay? The way you have the delay set up is a bit different from what I’m use to. When I chngae the _delay_loop_2(0); It is much longer than one second. About 5 seconds. Please share your thoughts. BTW your tuts are amazing! I wish I saw them earlier.

  3. Kamal Jadidy Aval
    Sunday 20 May 2012, 1:24 pm

    Dear sir,
    Unfortunately the code above has some problems while I try to view it.
    (May be some HTML conversion problem.)

    Would you please send me the code above?

    Regards,

  4. hay henrik,
    i m trying to do ultrasonic measurment system for car parking system
    by taking referance of ur system as my collage project.i dnt hv much idea about writing codes so i m going to use your code in it,so i wnted to ask you tht in the code i can see that LCD is connected across port B but i cant understand the right pin configuration for it,please help me with that,and one more thing that port B is not defined in code.being an amture in this field i am not able to understand the whole coding.please help me with the coding..

    • Henrik Sandaker Palm
      Monday 1 October 2012, 2:07 pm

      The configuration for the port is done in the LCD library which you need to include in your project (lcd.h). Sorry I cannot help you program.

  5. hello sir ….i am a 3rd yr engg student …..i am doing distance measurement as m y university project….. sir if u can please upload d circuit diagram of above kit …will b very helpful for me

  6. Hi Henrik!
    Could you add electrical schematics and firmware file in the format *.hex

  7. Better late than never…..

    To give a full schematic and code example on the HR-SR04. This might help: http://www.circuitdb.com/?p=1162

Leave a Reply