I want to generate two PWM signals VC and VH to give input to the gas sensor with the duty cycle mentioned below. I also want to take the reading from the same sensor at the same time using Arduino Uno.


I am confused how to proceed with the code in Arduino Uno.

  • I'd not use PWM for this. It's a pretty slow PWM signal, and you probably need to synchronize the measurement with the PWM signal. Just toggle the pins and add a delay. It would be helpful if you specified the part nr.
    – Gerben
    Jan 2, 2016 at 10:25
  • I cannot use the delay and toggle the pin because that will be running in a loop function and i want to perform some other function too in that loop function. And as far as i know, it will execute other statement only if it has completed the pulse generation. I have found this technique in Arduino site and it mentions that it has a second disadvantage - you can't leave the output running while the processor does something else.
    – Amitesh
    Jan 2, 2016 at 11:04
  • I don't understand what you are saying? All you need to do is turn on the heater, wait a bit, turn on the sensor, wait a bit, read the sensor, turn off the sensor, wait a bit, turn of the heater, do whatever calculation needs to be done in the remaining 236ms, and repeat. What is it exactly? Something like a TGS2442 CO sensor?
    – Gerben
    Jan 2, 2016 at 16:09
  • Thank you Gerben. Its TGS2444 ammonia sensor. But this is not what i want to do. I want to use the delay in reading my value, e.g. every 1 sec , etc. As i need to store the value for my future reference and analyse it. I don't want to store value after every 250ms. Along with this i will be using other sensors too like VOC's.
    – Amitesh
    Jan 3, 2016 at 1:12
  • 1
    Let me try. If it works i will inform you. Thank you.
    – Amitesh
    Jan 4, 2016 at 16:59

1 Answer 1


You can use the millis() function to see how much time has passed and use this to create your waveform while still being able to perform other tasks. You will check in which part of the wave you are in each iteration of loop() and adjust your outputs accordingly.


long vhtime;
uint8_t vhpin = 9;   // the VH output pin
uint8_t vcpin = 10;  // the VC output pin

long period = 250;   // the period of your pwm signal in ms
long vhduty = 14;    // amount of ms vh should be high
long vcdelay = 2;    // amount of ms we wait for vc to go high after vh has gone high
long vcduty = 5;     // amount of ms vc should be high

long curtime;        // this will hold our current position in one period of the waveform

void setup() {
  pinMode(vhpin, OUT);
  pinMode(vcpin, OUT);
  digitalWrite(vcpin, LOW);
  digitalWrite(vhpin, HIGH);
  vhtime = millis();

void loop() {
  curtime = millis() - vhtime;
  if (curtime > vhduty)        // we are in the last 236ms of the wave, vh should be low
    digitalWrite(vhpin, LOW);
  else                         // we are in the first 14ms of the wave, vh should be high
    digitalWrite(vhpin, HIGH);
  if (curtime > vcdelay && curtime < vcduty + vcdelay) // vc should be high during this time
    digitalWrite(vcpin, HIGH);
    digitalWrite(vcpin, LOW);
  if (curtime > period) {
    // we should start a new period
    digitalWrite(vhpin, HIGH);
    vhtime = millis();
  // do your other calculations here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.