Keyes KY 031 Hit Sensor


The Keyes KY 031 Hit Sensor Knock or Hit Sensor Keys 031 is a variation on the vibration sensors, except that the sensitivity is much reduced. It is designed to register a hit or contact on for example a robot.

The sketch below turns the onboard LED connected to pin 13 ON when the sensor is gently tapped. To allow the sensor to be simply plugged into the Arduino header I have used pins 6 and 7 to supply power, the sensor output being connected to pin 7. However, the sensor can just as easily be powered from the Gnd and 5 volt pins.

/********************************************************
Keyes KY 031 Hit or Knock Sensor
Chris Rouse December 2015
This sensor is less sensitive than the vibration sensors
Connections::
- on Sensor Board to Arduino Gnd (or pin 5)
Middle pin on Sensor Board to Arduino 5 volts (or pin 6)
S pin on Sensor Board to Arduino pin 7
********************************************************/
void setup() {
pinMode(13, OUTPUT); // onboard LED
digitalWrite(13, LOW); // turn off onboard LED
pinMode(5, OUTPUT); // connect to Vibration Sensor S pin
pinMode(6, OUTPUT); // connect to Vibration Sensor MIDDLE pin
pinMode(7, INPUT); // connect to Vibration Sensor - pin
digitalWrite(5, LOW); // Gnd connection
digitalWrite(6, HIGH); // 5 volts connection
}
void loop() {
if (digitalRead(7) == LOW){
digitalWrite(13, HIGH); // turn ON LED
delay(500);
}
else{
digitalWrite(13, LOW);
}
}

Advertisements

MQ2 Gas Sensor and Arduino

 

The MQ2 is one of a series of gas detectors and will detect flammable gasses and smoke. The topic of gas sensors is covered in the Arduino Playground here and you should visit this page for more details. This page also lists the various sensors in the range.

This blog records my efforts with this sensor. The small board I purchased from eBay has four pins, Vcc, Gnd, Analog Output and Digital output. Connect Vcc to 5 volts (do not try to power this from an Arduino Digital Output pin as the current drawn is too great. Connect Gnd to the Arduino Gnd. The sensor outputs a voltage proportional to the concentration of the flammable gas, while the Digital Output switches from High to Low when a certain level is reached. The level at which the DO pin switches is set using a small potentiometer on the board. For the sketch below connect the Analog Output to Arduino Analog Pin A0, and the Digital Output pin to Arduino Pin D2. The sketch will show the value of the voltage output and the state of the Digital Output, on the Serial Monitor (9600 baud).

To test the device I soaked a small piece of cotton wool in a flammable solvent and placed this at the bottom of a glass beaker. After five minutes I placed the sensor into the glass beaker and read the outputs from the Serial Monitor. The board I was using had an LED attached to the Digital Output, that came on when the output was greater than 400 (but this can easily be adjusted).

The sensor will get warm in use and can reach 50 or 60*C. I tested my setup by soaking a small piece of cotton wool in a solvent and placing it at the bottom of a glass beaker. After a few minutes I lowered the sensor into the beaker. I found different solvents gave different voltages, for example Ethanol gave a reading of about 250 to 300, while a low boiling petrol fraction gave a reading in excess of 600.

The manufacturer recommends that the sensor is not exposed to solvent vapour for extended periods.

The following sketch prints out the output voltage and state of the Digital Output, a buzzer could be substituted for the LED attached to the Digital output, but this could become a little annoying after a while.

/* GAS Sensor MQ-2
This sensor detects flammable gasses
the board has four pins
connect AO to Arduino pin A0
connect DO to Arduino pin 2
connect Gnd to Arduino Gnd
connect Vcc to Arduino 5 volts
*/

int sensorPin = A0; // select the input pin for the potentiometer
int DOPin = 2; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
int ledPin =13;

void setup() {
// declare the ledPin as an OUTPUT:
pinMode(DOPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}


void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
Serial.print("Analog Output = ");
Serial.println(sensorValue);
// turn the ledPin on if triggered
//
if (digitalRead(DOPin) ==HIGH){
digitalWrite(ledPin, LOW);
Serial.println("Digital Output = OFF");
}
else {
digitalWrite(ledPin, HIGH);
Serial.println("Digital Output = ON");
}
delay(1000);
}

TMP36 Temperature Sensor and Arduino

  
The TMP36 is an analogue sensor, that needs to be read using the Arduino’s analogue input. The sensor outputs a voltage that is proportional to the temperature and is packaged in a transistor like TO92 format. The sensor can operate at either 5 volts or 3 volts, but interestingly will produce greater accuracy when operated at 3 volts. Unfortunately because it is an analogue sensor it is difficult to use on a Raspberry Pi.

  

As the analogue input on the Arduino produces 1024 ‘steps’, so if the maximum voltage is 3.3 volts then each step is

 Voltage at pin in milliVolts = (reading from ADC) * (3300/1024) 

This formula converts the number 0-1023 from the ADC into 0-3300mV (= 3.3V)

Then, to convert millivolts into temperature, use this formula:

Centigrade temperature = [(analog voltage in mV) – 500] / 10

This formula allows for temperatures below zero.

The ADC in the Arduino uses an internal reference voltage of 5 volts, but this can be changed by using


analogReference(EXTERNAL)

Then connecting the new reference voltage to the AREF pin. In this case we connect the Arduino’s 3.3 volt output to AREF and the connections are shown in the diagram at the top of this page.

Upload the following code and switch to the Serial Monitor to see the temperature sensor in action.


/********************************************************
TMP36 temperature sensor test sketch
The TMP36 has three pins, with the flat on top and looking at the pins
Connect leftpin to Arduino 3.3v, and connect Arduino AREF to 3.3volts
Connect centre pin to Arduino pin 15 (A1)
Connect right pin to Arduino Gnd
********************************************************/

#define aref_voltage 3.3 // Connect 3.3V to ARef

//TMP36 Pin Variables
int tempPin = 1; //the analog pin the TMP36's Vout (sense) pin is connected to

//the resolution is 10 mV / degree centigrade with a
//500 mV offset to allow for negative temperatures
int tempReading; // the analog reading from the sensor

void setup(void)
{
Serial.begin(9600);
analogReference(EXTERNAL); // If you want to set the aref to something other than 5v
}

void loop(void) {

tempReading = analogRead(tempPin);
Serial.print("Temp reading = ");
Serial.print(tempReading); // the raw analog reading

// converting that reading to voltage, which is based off the reference voltage
float voltage = tempReading * aref_voltage;
voltage /= 1024.0;

// print out the voltage
Serial.print(" - ");
Serial.print(voltage); Serial.println(" volts");

// now print out the temperature
float temperatureC = (voltage - 0.5) * 100 ; //converting from 10 mv per degree with 500 mV offset
//to degrees ((volatge - 500mV) times 100)
Serial.print(temperatureC); Serial.println(" degrees C");

// now convert to Fahrenheight
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
Serial.print(temperatureF); Serial.println(" degrees F");
delay(1000);
}