Back to Getting Started

Back to Examples

This example demonstrates setting up and making use of the Motion sensor Board

This examples helps you in learning APIs and application firmware that can be used to:

1. To figure out the motion sensor functionality and therefore can be used to build an application to detect motion.


Before you proceed with this tutorial, you must have Koliada visual studio plugin and Koliada toolchain setup. For more information on the setup check out Koliada Getting started. This tutorial is currently based on the TI CC2541 which uses the 8051.

In this example we will be detecting the motion based on the state change from the motion sensor. The sensor outputs a state of HIGH-1(if motion is detected) or LOW-0(no motion). To route the output of the motion sensor through the SPIP pin 7 or SPIP pin 8, we need to remove the R1 or R2 resistors respectively. Please make sure you remove the resistor to use the pin you desire.

Note - Removing both the resistors R1 and R2 will not route the output through either of the pins. So make sure you only remove the resistor for the pin you want to use.

Before working on this example, make sure you have set the latest KoliadaESDK(TM). More information on setSDK setup and setting the path is shown here.

Note: After making sure the SDK path is set, you must know that the board config must be flashed only if you are using any I/O pins. In this particular example there are no I/O configurations for the motion sensor, so you can directly flash the application firmware.

Select the SDK using the SDK Dropdown button at the top.

You can find the application firmware for motion sensor under KoliadaESDK(TM)→examples→motionTest.

Now compile the project and hit download to target.

You should see the following output on your serial terminal

In this example we should be testing the operation of the motion detection.

As you can see from the above output image, there will be no motion detected when there is no movement and the sensor state is being continously read every timer interval and the status is printed out on the screen. For more information on the motion sensor functionality, refer to Datasheet.

You can now wave your hand in the sight of the sensor and you should see the following output of motion detected:

We can walkthrough motionTest.c now.

Application Firmware (motionTest)

The main.c calls motionTest() function in motion.c

  void motionTest()
	printf(">>%s\n", __func__); 
        objectCreate(motionTimer, kIntervalTimer, TICKS(1000));
	OnEvent(motionTimer, (HANDLER) motionTimerHandler);
	// wait for system events
	WaitEvent(0, 0);// never returns

The objectCreate(motionTimer, kIntervalTimer, TICKS(1000)); creates a timer under the name motionTimer(which is supposed to happen when ever every second). On this timer event (OnEvent(motionTimer, (HANDLER) motionTimerHandler);) the motionTimerHandler will be called. Also once the timer event is defined and eventhandlers are set for motiontimer we need to enable/start the timer using cmStartTimer(motionTimer).

Now since all the events are set the system goes to event listening mode. This is done by using WaitEvent(0, 0); .

void motionTimerHandler(TIMER t)
    long a, vdd;
    word v;
   a = adc(0x80 | 0x30 | 0x05) >> 3; // adc config to read motion voltage
   vdd = (((a * 1000)/4096 * 1150 * 3)+500)/1000;
   v = vdd/1000;
  // printf("the voltage on the line is %d.%ld v\n", v, vdd-v *1000);
	if( (vdd < 2000) && (motionCount == 0))
	else if(vdd > 2000)
	  debug("\n---MOTION DETECTED---");
	  motionCount = 3;

We setup the adc line to measure the motion sensor output voltage through SPIP pins using adc(0x80 | 0x30 | 0x05) » 3.

Based on the voltage we print the status as motion detected or no motion. We have now accomplished setting up and accessing the motion sensor. Now you can try setting up a different application using the sensor and observe your results.

More Examples

More Boards

      Copyright © 2018-2019 Koliada, LLC
  • piep/examples/motion.txt
  • Last modified: 2019/04/16 01:12
  • by venkat