Skip to content

Peripherals: Sensors

Stefan Lenz edited this page Dec 27, 2022 · 3 revisions

A word on sensors

There are countless sensors for Arduino systems. Some can be simply accessed with the analog or digital outputs of an Arduino as they generate only simple output messages. Typical examples are humidity sensors, light sensors and PIR sensors.

PIR Sensor

Other sensors need complex serial protocols to access then. There are many libraries around for Arduinos to read these sensors. The quality of these libraries varies. Some are very good but even among the common ones there are a few bad apples. While it would be possible to read sensors directly from BASIC, a generic interface for sensor code has been added.

The following sensors are supported right now

  1. DHT11 and DHT22 environment sensors
  2. SHT3x environment sensors
  3. MQ2 gas sensors
  4. LSM6 gyroscopic sensors
  5. AHT10 environment sensors
  6. BMP280 environment sensors
  7. BME280 environment sensors

From the list it is clear how I use BASIC mostly: environmental monitoring applications.

Compile BASIC with the sensor code

Define the macro ARDUINOSENSORS in the hardware definition code. Then look for the sensor definition code section

#ifdef ARDUINOSENSORS

#undef ARDUINODHT

#define DHTTYPE DHT22

#define DHTPIN 2

#undef ARDUINOSHT

#undef ARDUINOMQ2

#define MQ2PIN A0

#undef ARDUINOLMS6

#undef ARDUINOAHT

#undef ARDUINOBMP280

#undef ARDUINOBME280

#endif

If you need one of the sensors, also set the macro here to define. Make sure that you have the necessary library installed and that pins are defined correctly for non I2C sensors.

Using sensors

Sensors are accesses with the SENSOR function. The function has two arguments. The first one is the sensor number just like in the list above, the second argument is the value requested from the sensor. A typical sensor read for the DHT sensor would look like this:

PRINT SENSOR(1, 1)

This would output the humidity of a DHT sensor. Supported sensor values are currently

  1. DHT11 and DHT22: humidity SENSOR(1, 1) and temperature SENSOR(1, 2)
  2. SHT3x environment sensors: humidity SENSOR(2, 1) and temperature SENSOR(2, 2)
  3. MQ2 gas sensors: LPG gas level SENSOR(3, 1), CO level SENSOR(3, 2), smoke level SENSOR(3, 3)
  4. AHT10 environment sensors: temperature SENSOR(4, 1) and humidity SENSOR(4, 2)
  5. BMP280 environment sensors: temperature SENSOR(5, 1), pressure SENSOR(5, 2), altitude SENSOR(5, 3)
  6. BME280 environment sensors: temperature SENSOR(5, 1), pressure SENSOR(5, 2), altitude SENSOR(5, 3), humidity SENSOR(5, 4)

For all sensors, SENSOR(n, 0) returns 1 if the sensor is compiled into the code and 0 if the sensor is absent.

Libraries

The libraries used in the code are all standard libraries from the Arduino library manager.

  • DHT library: DHT.h
  • SHT library: SHT3x.h
  • MQ2 library: MQ2.h
  • AHT library: Adafruit_AHTX0.h
  • BMP280 library: Adafruit_BMP280.h
  • BME280 library: Adafruit_BME280.h

Adding your own sensors

To add your own sensor, please modify the functions sensorbegin() and sensorread() in BASIC. They are in the Arduino sensor library code section.