drivers/mq3: added driver for alcohol sensor

This commit is contained in:
Hauke Petersen 2014-09-24 13:27:13 +02:00
parent d7381cfd13
commit 5bd6155ca6
4 changed files with 122 additions and 0 deletions

View File

@ -46,5 +46,8 @@ endif
ifneq (,$(filter isl29020,$(USEMODULE)))
DIRS += isl29020
endif
ifneq (,$(filter mq3,$(USEMODULE)))
DIRS += mq3
endif
include $(RIOTBASE)/Makefile.base

72
drivers/include/mq3.h Normal file
View File

@ -0,0 +1,72 @@
/*
* Copyright (C) 2014 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for more
* details.
*/
/**
* @defgroup driver_mq3 MQ-3 Alcohol Tester
* @ingroup drivers
* @brief Device driver for the MQ-3 alcohol sensor
* @{
*
* @file
* @brief Device driver interface for the MQ-3 alcohol sensor
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*/
#ifndef __MQ3_H
#define __MQ3_H
#include "periph/adc.h"
#define MQ3_MAX_RAW_VALUE (1023U)
/**
* @brief device descriptor for a MQ-3 sensor
*/
typedef struct {
adc_t adc_dev; /**< the used ADC device */
int adc_chan; /**< used channel of the ADC */
} mq3_t;
/**
* @brief Initialize a MQ-3 alcohol sensor
*
* The MQ-3 sensor is interfaced by a single ADC pin, specified by `adc` and `channel`.
*
* @note The sensor needs about a minute to heat up before meaningful measurements
* can be made.
*
* @param[out] dev device descriptor of an MQ-3 sensor
* @param[in] adc the ADC device the sensor is connected to
* @param[in] channel the channel of the ADC device used
*
* @return 0 on success
* @return -1 on error
*/
int mq3_init(mq3_t *dev, adc_t adc, int channel);
/**
* @brief Read the RAW sensor value, can be between 0 and MQ3_MAX_RAW_VALUE
*
* @param[in] dev device descriptor of the MQ-3 sensor to read from
*
* @return the raw sensor value, between 0 and MQ3_MAX_RAW_VALUE
*/
int mq3_read_raw(mq3_t *dev);
/**
* @brief Read the scaled sensor value of PPM of alcohol
*
* @param[in] dev device descriptor of the MQ-3 sensor to read from
*
* @return the scaled sensor value in PPM of alcohol
*/
int mq3_read(mq3_t *dev);
#endif /* __MQ3_H */
/** @} */

3
drivers/mq3/Makefile Normal file
View File

@ -0,0 +1,3 @@
MODULE = mq3
include $(RIOTBASE)/Makefile.base

44
drivers/mq3/mq3.c Normal file
View File

@ -0,0 +1,44 @@
/*
* Copyright (C) 2014 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License v2.1. See the file LICENSE in the top level directory for more
* details.
*/
/**
* @ingroup driver_mq3
* @{
*
* @file
* @brief Device driver implementation for the MQ-3 alcohol sensor
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
*
* @}
*/
#include "mq3.h"
#define PRECISION ADC_RES_10BIT
#define MIN (100U) /* TODO: calibrate to useful value */
#define FACTOR (2.33f) /* TODO: calibrate to useful value */
int mq3_init(mq3_t *dev, adc_t adc, int channel)
{
dev->adc_dev = adc;
dev->adc_chan = channel;
return adc_init(dev->adc_dev, PRECISION);
}
int mq3_read_raw(mq3_t *dev)
{
return adc_sample(dev->adc_dev, dev->adc_chan);
}
int mq3_read(mq3_t *dev)
{
float res = mq3_read_raw(dev);
res = (res > MIN) ? res - MIN : 0;
return (int)(res * FACTOR);
}