From 5bd6155ca6ef643ad6d62092149378777b4f3329 Mon Sep 17 00:00:00 2001 From: Hauke Petersen Date: Wed, 24 Sep 2014 13:27:13 +0200 Subject: [PATCH] drivers/mq3: added driver for alcohol sensor --- drivers/Makefile | 3 ++ drivers/include/mq3.h | 72 +++++++++++++++++++++++++++++++++++++++++++ drivers/mq3/Makefile | 3 ++ drivers/mq3/mq3.c | 44 ++++++++++++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 drivers/include/mq3.h create mode 100644 drivers/mq3/Makefile create mode 100644 drivers/mq3/mq3.c diff --git a/drivers/Makefile b/drivers/Makefile index b0bec61f72..bb0312148d 100644 --- a/drivers/Makefile +++ b/drivers/Makefile @@ -46,5 +46,8 @@ endif ifneq (,$(filter isl29020,$(USEMODULE))) DIRS += isl29020 endif +ifneq (,$(filter mq3,$(USEMODULE))) + DIRS += mq3 +endif include $(RIOTBASE)/Makefile.base diff --git a/drivers/include/mq3.h b/drivers/include/mq3.h new file mode 100644 index 0000000000..82717e83cc --- /dev/null +++ b/drivers/include/mq3.h @@ -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 + */ + +#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 */ +/** @} */ diff --git a/drivers/mq3/Makefile b/drivers/mq3/Makefile new file mode 100644 index 0000000000..c602b8fe31 --- /dev/null +++ b/drivers/mq3/Makefile @@ -0,0 +1,3 @@ +MODULE = mq3 + +include $(RIOTBASE)/Makefile.base diff --git a/drivers/mq3/mq3.c b/drivers/mq3/mq3.c new file mode 100644 index 0000000000..e3fe6285e7 --- /dev/null +++ b/drivers/mq3/mq3.c @@ -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 + * + * @} + */ + +#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); +}