Saturday, March 29, 2014

CO (Carbon Monoxide) Gas Sensor Using the Arduino Uno

This simple project uses the Arduino Uno and the MQ7 Gas Sensor to sense the concentration of CO (Carbon Monoxide) in the air. The MQ7 requires a heater voltage that cycles between 5v (60s) and 1.4v (90s), drawing approximately 150mA at 5v which exceeds the power capacity of the Uno, so I use the KA278RA05C adjustable voltage regulator to drive this. The default voltage of the KA278RA05C with Vadj (pin 4) disconnected is 5v which serves for the heater high voltage part of the cycle. I use a 50k potentiometer to adjust the voltage down to 1.4v for the heater high voltage part of the cycle. I use an LH1546 optical solid state relay to switch the adjustable voltage of the potentiometer on for the 1.4v heater low voltage. Pin 8 on the Arduino Uno drives the optical solid state relay and when high turns the relay on, adjusting the voltage of the regulator down to 1.4v. When this pin is low it turns off the relay causing the regulator to go back up to 5v. Analog pin 0 on the Arduino Uno is used to sense the voltage level out of the MQ7 which serves to measure the concentration of CO (Carbon Monoxide) in the air. Below is the diagram of the circuit on Fritzing.

Below is the actual circuit:

Below is the Arduino Uno sketch used:

#define VOLTAGE_REGULATOR_DIGITAL_OUT_PIN 8
#define MQ7_ANALOG_IN_PIN 0

#define MQ7_HEATER_5_V_TIME_MILLIS 60000
#define MQ7_HEATER_1_4_V_TIME_MILLIS 90000

#define GAS_LEVEL_READING_PERIOD_MILLIS 1000

unsigned long startMillis;
unsigned long switchTimeMillis;
boolean heaterInHighPhase;

void setup(){
  Serial.begin(19200);
  
  pinMode(VOLTAGE_REGULATOR_DIGITAL_OUT_PIN, OUTPUT);
  
  startMillis = millis();
  
  turnHeaterHigh();
  
  Serial.println("Elapsed Time (s), Gas Level");
}

void loop(){
  if(heaterInHighPhase){
    // 5v phase of cycle. see if need to switch low yet
    if(millis() > switchTimeMillis) {
      turnHeaterLow();
    }
  }
  else {
    // 1.4v phase of cycle. see if need to switch high yet
    if(millis() > switchTimeMillis) {
      turnHeaterHigh();
    }
  }
  
  readGasLevel();
  delay(GAS_LEVEL_READING_PERIOD_MILLIS);
}

void turnHeaterHigh(){
  // 5v phase
  digitalWrite(VOLTAGE_REGULATOR_DIGITAL_OUT_PIN, LOW);
  heaterInHighPhase = true;
  switchTimeMillis = millis() + MQ7_HEATER_5_V_TIME_MILLIS;
}

void turnHeaterLow(){
  // 1.4v phase
  digitalWrite(VOLTAGE_REGULATOR_DIGITAL_OUT_PIN, HIGH);
  heaterInHighPhase = false;
  switchTimeMillis = millis() + MQ7_HEATER_1_4_V_TIME_MILLIS;
}

void readGasLevel(){
  unsigned int gasLevel = analogRead(MQ7_ANALOG_IN_PIN);
  unsigned int time = (millis() - startMillis) / 1000;
  
  Serial.print(time);
  Serial.print(",");
  Serial.println(gasLevel);
}



The screenshot below shows the CSV data points of time in seconds against the MQ7 output voltage graphed into Excel.

The point at which to read the MQ7 level is at the end of the high 5v heating phase just before transitioning to the low 1.4v heating voltage. I found mine stabilized in an indoor home environment around 211 after the second heating cycle, corresponding to an analog voltage out of the MQ7 of (211/2013) * 5v = 1.03v. To calibrate I'll need a CO gas meter. Next item on the purchase list :-) Enjoy. Feel free to post below if you build some additional cool stuff on this.

If you are interested in how this type of sensor can be integrated into a broader solution that includes notification of carbon monoxide on a users Android phone see Receive Alert of High Carbon Monoxide Level

Saturday, February 22, 2014

High Sensitivity Vibration Sensor Using Arduino

In my last post I described how to build a High Sensitivity Arduino Sound Level Detector. Another useful type of sensor to determine if something interesting is going on in the environment is a vibration sensor. In this post I use a piezo element as a raw sensor to detect vibration.

I found the raw piezo generated a very small signal. To greatly improve its sensitivity I used epoxy to glue a fishing weight to the piezo sensor. The piezo drives a load resistor of 1M in parallel with a 5.1v Zener diode just to protect the IC's against any large voltage spikes in the event of a large physical bump. I found the raw output of the piezo unsuitable for direct input to the Arduino as it is typically a very small voltage signal and needs amplification, so I amplify the signal from the piezo with a 221 gain non-inverting op-amp using one side of an LM358. I use the other side of the LM358 for a comparator. The sensitivity of the vibration sensor is controlled using a potentiometer for the threshold (negative) input into the comparator. The other (positive) input to the comparator comes from the amplifier of the piezo signal. The output of the comparator provides a direct input to Arduino Uno digital pin 8. To hear when it senses vibration I use a simple piezo buzzer driven directly from Arduino Uno pin 13. Below is the circuit diagram:

... and the breadboard circuit:

Here is the actual prototype:

... and a close up of the piezo element with the fishing weight glued on with epoxy for added sensitivity:

Here is the sketch I used on the Arduino Uno:

If you want to use this as a starting point you can copy / paste from below:

#define VIBRATION_DIGITAL_IN_PIN 8
#define BUZZER_DIGITAL_OUT_PIN 13

int buzzerDurationMillis = 1000;

void setup(){
  pinMode(VIBRATION_DIGITAL_IN_PIN, INPUT);
  pinMode(BUZZER_DIGITAL_OUT_PIN, OUTPUT);
}

void loop(){
    if(digitalRead(VIBRATION_DIGITAL_IN_PIN) == HIGH){
      digitalWrite(BUZZER_DIGITAL_OUT_PIN, HIGH);
      delay(buzzerDurationMillis);
      digitalWrite(BUZZER_DIGITAL_OUT_PIN, LOW);
    }
}
Enjoy. Let me know if you make any cool improvements on this.

Note: if you notice your output locking in on state try lowering the feedback resistor of the op-amp from 220k to something lower, for example 160k.

Update: I later added a 0.1uF capacitor to connect the output from the piezo element to the input of the op-amp, also grounded on the op-amp side using a 100k resistor. This acted as a DC decoupler and effectively lowered the comparator threshold required to detect vibration.

Below is the updated circuit diagram showing the refinements for reducing the gain to avoid op-amp output lockup, and DC decoupler on the input.

If you are wanting to differentiate single bumps from vibrations you may also be interested in this blog I did on an Arduino sketch that can be used to avoid false positives. You may also want to look at my subsequent blog on how to use these in an open door or tailgate down sensor which also uses a Reed switch.

If you are interested in how this type of sensor can be integrated into a broader solution that includes notification of detected vibration on a users Android phone see Detect Intrusion with Passive Infrared, Sound, or Vibration

Monday, February 17, 2014

High Sensitivity Arduino Sound Level Detector

Generally we want to sense the environment when something interesting is occurring. Sometimes the presence of sound is indicative of an interesting activity. If we can detect sound level we can trigger a sensing activity to capture information about the activity of interest. I have posted a short video of this simple high sensitivity Arduino sound level detector working.

In this project we use an Arduino Uno, an electret microphone and an LM358 dual operational amplifier to create a simple sound level detector. The signal from the mic is amplified by the one side of the LM358 with a gain of approximately 221 (see op-amp wiki) as defined by the 220k feedback resistor and the 1k pull down resistor connected to the negative input of the LM258 amplifier. The output of the first stage amplifier provides input to the other side of the LM358 used as a comparator. The triggering threshold of the comparator is controlled using the potentiometer. When the audio signal from the first amp exceeds the triggering threshold the comparator sends a digital '1' to the Arduino Uno on pin 8. When the Uno detects the sound level high input it turns an LED on. You can see a schematic diagram of the sound detection part of the circuit below.

Prototyping this on a breadboard looks something like this:

The actual breadboard prototype appears below:

... and the simple sketch I used to drive the Arduino Uno appears below:

I include the sketch code below if you'd like to try it out yourself:

#define SOUND_DETECTED_DIGITAL_IN_PIN 8
#define LED_DIGITAL_OUT_PIN 7

void setup(){
  pinMode(SOUND_DETECTED_DIGITAL_IN_PIN, INPUT);
  pinMode(LED_DIGITAL_OUT_PIN, OUTPUT);
}

void loop(){
  if(digitalRead(SOUND_DETECTED_DIGITAL_IN_PIN) == HIGH){
    digitalWrite(LED_DIGITAL_OUT_PIN, HIGH);
    delay(50); // delay long enough for you to see the LED on
  }
  else {
    digitalWrite(LED_DIGITAL_OUT_PIN, LOW);
  }
}

Stay tuned for projects that receive the sound detection as input and use it to drive other interesting activities. Have fun. Feel free to share below if you built something cool with this, or have some ideas for enhancements.

You may also be interested in a high sensitivity vibration sensor using the Arduino Uno.

Note: if you notice your output locking in on state try lowering the feedback resistor of the op-amp from 220k to something lower, for example 160k.

If you are interested in how this type of sensor can be integrated into a broader solution that includes notification of detected sound level on a users Android phone see Detect Intrusion with Passive Infrared, Sound, or Vibration

Friday, November 1, 2013

Is Healthcare Ready for Wearables?

Wearables promise major benefits to healthcare workers in providing new powerful information sources at the point of care, promising hands free operation that enables improved patient care, efficiency and productivity. Research has shown that what users do with technology is a major source of privacy and security risk. While some risk comes from vulnerabilities in the device hardware or software, it is what users do with technology that drives much if not most of the risk. In many cases this is inadvertent, with users unaware of such privacy and security risk side effects. To date, the source of much of this risk is from mobile devices and apps. An example is a healthcare worker using a file transfer app to transfer sensitive personal information such as patient records to a co-worker. While this makes exchange of healthcare information easy and efficient, it subjects such information to risks of breach. It can also compromise the integrity of the master patient record since such transfers and updates often don't update the master patient record, leading to a record that is incomplete or out of date, which in turn can result in suboptimal healthcare, or in a worst case a patient safety issue. Such risks also often have a cloud component, where for example a file transfer involves moving and storing patient information in a cloud, outside the control of the healthcare organization. This side effect is often called BYOC (Bring Your Own Cloud), and aside from confidentiality and integrity risks can also introduce trans border data flow risks. This type of risk is set to increase as users are further empowered with increasingly powerful devices, apps, wearables. Breaches have had major negative impacts on healthcare. Realizing the amazing benefits of these new technologies while minimizing these risks requires a proactive approach in which need to understand the lifecycle and flow of personal information around these devices, anticipate risks, avoid such risks wherever possible, and otherwise make informed and reasonable benefit / risk tradeoffs.

Many wearables are not full stand-alone computing devices in and of themselves, but are closer to advanced IO devices connected wirelessly to a nearby device such as a smartphone. An imminent example of this is Google Glasses. These types of wearables are capable of recording vast quantities of photos, audio and video that exceed the storage and processing capabilities of the local smartphone and therefore will be uploaded to the cloud. Further, this upload to the cloud could be over a personal 4G wireless network that doesn't even touch the healthcare organizations network and so can't be detected or blocked. Envision a patient or caregiver walking into a waiting room, or around a hospital, wearing one of these, recording other patients. While we have precedents of BYOC with mobile devices and apps, wearables such as this are poised to increase this problem drastically. Mitigating this type of risk will be a challenge, and will require a holistic approach consisting of technical safeguards to automatically detect and alert to this type of use and risk, as well as administrative controls such as policy and training, as well as physical controls that prevent the presence and use of such devices in particularly sensitive settings. The initial period after appearance of such wearables is bound to be bumpy, with many unfortunate negative surprises. Ultimately, a new set of social norms must be established that minimize these and streamline the use and benefits of wearables while minimizing risks.

What types of risks are you seeing with wearables in healthcare?