All projects
04Live

Ultrasonic Distance

Measure distance with sound; map proximity to LED color.

TimingPulse
01

What you learn

Computing distance by sending a pulse and timing the echo with a timer. The sensor works like a bat — it sends sound and listens for the echo.

02

Parts you need

03

Wiring

ModuleBoardNote
VCC3.3VNot 5V! A 5V echo would damage the chip.
GNDGND
TrigPB2Trigger output
EchoPB3Echo input
04

Code

main.c
1#include <stdint.h>
2#include <stdbool.h>
3#include "inc/hw_memmap.h"
4#include "driverlib/sysctl.h"
5#include "driverlib/gpio.h"
6#include "driverlib/timer.h"
7 
8#define LED_ALL (GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3)
9#define TRIG GPIO_PIN_2 // PB2
10#define ECHO GPIO_PIN_3 // PB3
11#define PWM_STEPS 256
12 
13// Software PWM: show the color for 'periods' cycles
14static void showColor(uint8_t r, uint8_t g, uint8_t b, uint32_t periods) {
15 volatile uint32_t p, i;
16 for (p = 0; p < periods; p++)
17 for (i = 0; i < PWM_STEPS; i++) {
18 uint8_t out = 0;
19 if (i < r) out |= GPIO_PIN_1;
20 if (i < g) out |= GPIO_PIN_3;
21 if (i < b) out |= GPIO_PIN_2;
22 GPIOPinWrite(GPIO_PORTF_BASE, LED_ALL, out);
23 SysCtlDelay(2);
24 }
25}
26 
27static void delayUs(uint32_t us) { SysCtlDelay((SysCtlClockGet() / 3000000) * us); }
28 
29// One measurement -> distance in cm (0 = no echo / out of range)
30static uint32_t measureCm(void) {
31 uint32_t start, end, timeout;
32 
33 GPIOPinWrite(GPIO_PORTB_BASE, TRIG, TRIG); // 10 us trigger pulse
34 delayUs(10);
35 GPIOPinWrite(GPIO_PORTB_BASE, TRIG, 0);
36 
37 timeout = 600000; // wait for echo to go high
38 while (GPIOPinRead(GPIO_PORTB_BASE, ECHO) == 0)
39 if (--timeout == 0) return 0;
40 start = TimerValueGet(TIMER0_BASE, TIMER_A);
41 
42 timeout = 600000; // wait for echo to go low
43 while (GPIOPinRead(GPIO_PORTB_BASE, ECHO) != 0)
44 if (--timeout == 0) return 0;
45 end = TimerValueGet(TIMER0_BASE, TIMER_A);
46 
47 return ((end - start) / 50) / 58; // 50 MHz -> us, then us/58 = cm
48}
49 
50int main(void) {
51 SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL |
52 SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
53 
54 SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
55 while (!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOF)) {}
56 GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, LED_ALL);
57 
58 SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
59 while (!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOB)) {}
60 GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, TRIG);
61 GPIOPinTypeGPIOInput(GPIO_PORTB_BASE, ECHO);
62 
63 SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); // free-running us timer
64 while (!SysCtlPeripheralReady(SYSCTL_PERIPH_TIMER0)) {}
65 TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC_UP);
66 TimerLoadSet(TIMER0_BASE, TIMER_A, 0xFFFFFFFF);
67 TimerEnable(TIMER0_BASE, TIMER_A);
68 
69 while (1) {
70 uint32_t cm = measureCm();
71 if (cm == 0 || cm > 60) {
72 showColor(0, 0, 40, 20); // no reading -> dim blue
73 } else {
74 if (cm < 2) cm = 2;
75 if (cm > 50) cm = 50;
76 uint8_t t = (uint8_t)(((cm - 2) * 255) / 48);
77 showColor(255 - t, 0, t, 20); // near = red, far = blue
78 }
79 SysCtlDelay(SysCtlClockGet() / 30);
80 }
81}
05

Output

The simulation below runs in your browser — it mimics the real board.

distance
25 cm
echo time
1450 us
RGB
133,0,122