External Components

ADC Ultrasonic Proximity Sensor

#include “adc0.h”

int main(void)
{
    int reading = 0;

    // Initialization :
    LPC_PINCON->PINSEL3 |=  (3 << 28); // ADC-4 is on P1.30, select this as ADC0.4

    while (1)
    {
        reading = adc0_get_reading(4); // Read current value of ADC-4
        printf("\nADC Reading: %d", reading);
        delay_ms(1000);
    }

    return 0;
}

Buzzer

A buzzer needs a PWM to operate, or you can operate it using a GPIO but it can waste your CPU cycles. Before you start with the code, attach a GPIO or PWM pin to a 1K resistor leading to the buzzer's red wire. The black wire should be connected to ground.

#include "gpio.hpp"
#include "utilities.h"

int main(void)
{
	/* Assume we attached our P1.20 to the buzzer */
	GPIO buzzer(P1_20);   /* Use P1.20 as General Purpose Input/Output (GPIO) */
	buzzer.setAsOutput(); /* Use this pin as OUTPUT */

	while (1)
	{
		/* Beep for one second */
		for (int i = 0; i < 1000; i += 2)
		{
			delay_ms(1);
			buzzer.setHigh();
			delay_ms(1);
			buzzer.setLow();
		}
	}
}

Motion Sensor

A typical motion sensor like a PIR motion sensor is very easy to attach. Power-up the sensor using 5v, and connect the ground. Then take a resistor, tie one end to 5v, and the other end to the motion's signal. Finally, tie the signal to one of your GPIOs.

#include "gpio.hpp"
#include "utilities.h"

int main(void)
{
	/* Assume we attached our P1.20 to out motion sensor's output pin */
	GPIO motion(P1_20);   /* Use P1.20 as General Purpose Input/Output (GPIO) */
	motion.setAsInput();
	motion.enablePullUp();

	while (1)
	{
		/* Get average of one second */
		int count = 0;
		for (int i = 0; i < 1000; i++)
		{
			/* If output is low, then motion has been detected, but it could be false positive */
			if (!motion.read())
			{
				count++;
			}
			delay_ms(1);
		}
		/* Test and calibrate yourself: */
		bool motionDetected = (count > 50);
	}
}
Back to top