This is an old revision of the document!


Related to: [HW] Sensors Module

This library contains functions to use different sensors in the HomeLab kit.

Data Types

  • ir_distance_sensor

Infrared distance sensor distance calculation parameters structure. Formula for distance calculation is a / (ADC + b) - k. Structure members:

  • a - Dividend.
  • b - Non-linear constant.
  • k - Linearizing constant.


  • ir_distance_sensor GP2Y0A21YK

Sharp GP2Y0A21YK distance calculation formula parameters.


  • signed short thermistor_calculate_celsius(unsigned short adc_value)

Calculates thermistor temperature in Celsius degrees from ADC conversion result. Functions use pre-calculated conversion table. Parameters:

  • adc_value - ADC conversion result (10-bit with +5 V reference voltage)..
  • Returns temperature in Celsius degrees in the -20 to 100 degrees limit.
  • signed short ir_distance_calculate_cm(ir_distance_sensor sensor, unsigned short adc_value)

Calculates distance from ADC result received from IR distance sensor voltage. Parameters:

  • sensor - Distance sensor calculation parameters.
  • adc_value - ADC conversion result (10-bit with +5 V reference voltage).
  • Returns distance in centimeters or -1 if it cannot be calculated.
  • unsigned short ultrasonic_measure(pin trigger, pin echo)

Measures distance with ultrasonic distance sensor. Functions generate a trigger pulse on one pin and measures the time of echo pulse on another pin. Distance is calculated from the time. Function expects a 14.7456 MHz clock frequency. Measuring may take up to 36 ms. Parameters:

  • trigger - Trigger pin variable.
  • echo - Echo pin variable.
  • Returns distance in centimeters or 0 when measuring failed.


The following program demonstrates usage of IR and ultrasonic distance sensors.

#include <homelab/module/sensors.h>
// Ultrasonic distance sensor control pins.
pin pin_trigger = PIN(G, 1);
pin pin_echo    = PIN(G, 0);
int main(void)
	unsigned short adc_value = 400; // random ADC result.
	signed short distance;
	// Distance calculation from IR distance sensor ADC result.
	distance = ir_distance_calculate_cm(GP2Y0A21YK, adc_value);
	// Measuring with ultrasonic distance sensor.
	distance = ultrasonic_measure(pin_trigger, pin_echo);
en/software/homelab/library/module/sensor.1293710247.txt.gz · Last modified: 2013/03/26 23:02 (external edit) Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0