CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

MQ-2 Sensor
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
fiasgardone



Joined: 19 Jan 2010
Posts: 71

View user's profile Send private message

MQ-2 Sensor
PostPosted: Sat Apr 18, 2015 2:02 pm     Reply with quote

Hi!!

I'm doing tests with a MQ-2 gas sensor, but the code below does not work ยจ
What I really want to do, is when the sensor detects over 25% of gas lights a LED on pin, but the code compiles fine without error but physically does not work.

I hope you can help me where I'm wrong!

Thank you very much

Code:

/*
Product Description
MQ-2 Gas Sensor Module Smoke Methane Butane Detection

  Chip: LM393, ZYMQ-2 gas sensors
  Working voltage: DC 5V
  with a signal output instruction.
  dual signal output (analog output, and TTL level output)
  TTL output
  0~5V analog output voltage, the higher the concentration the higher the voltage.
  the gas, natural gas, city gas, smoke better sensitivity.
  with a long service life and reliable stability
*/

 #include <18F458.H>
 #device adc=10
 #fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
 #use delay(clock=20000000)


 #define LED_PIN  PIN_B0
 
 
 //==========================================
 void main()
 {
 int8 adc_result;
 
 setup_adc_ports(AN0);
 setup_adc(ADC_CLOCK_DIV_8);
 set_adc_channel(0);
 delay_us(20);

 float new;
 float val=0.00122; //25% Input ADC
 new=adc_result;

 output_low(LED_PIN); // Set LED to Off

 while(1)
   {
    adc_result = read_adc();
     
    if(adc_result !=new && new >= val) {
       output_high(LED_PIN);
       delay_ms(100);
       adc_result=new; }
   }

 }

Ttelmah



Joined: 11 Mar 2010
Posts: 19447

View user's profile Send private message

PostPosted: Sat Apr 18, 2015 2:10 pm     Reply with quote

First, have you actually checked the PIC is running?.
Without any testing, just have a loop flashing the LED. Does it flash?. At the right rate?.

Always prove the chip is working _first_.

Then there are problems with your variables. You have the ADC set to return a 10bit value (0 to 1023), but are putting the result into an 8bit integer....

Then you are not initialising the values. Adc_result, and new, you load, but the variable 'val' will probably be some enormous undefined number. So 'new' won't ever get larger than it.

Then get rid of the floats. No point in using them. int16 for all the variables.

int16 new,val=0,adc_result;

Then you never turn the LED off, once it does go on.
fiasgardone



Joined: 19 Jan 2010
Posts: 71

View user's profile Send private message

PostPosted: Sat Apr 18, 2015 3:11 pm     Reply with quote

Thank you for your answer Ttelmah!

Yes, the chip works very well, did tests and works 100%.

Modified the variables as described, and the ADC is not working!
I used variables float, because with adc = 10 to converter input ADC 5/1023 = 0.004887 and I want the LED only lights up when it's 25%
the analog Input.

I think all the error is in the analog input, but do not know what I'm doing wrong it should not work!
I hope someone can enlighten me how to solve this! I do not know do better.

Thank you very much

Code:
 #include <18F458.H>
 #device adc=10
 #fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
 #use delay(clock=20000000)


 #define LED_PIN  PIN_B0
 
 
 //==========================================
 void main()
 {
 int16 new,val=0,adc_result;
 
 setup_adc_ports(AN0);
 setup_adc(ADC_CLOCK_DIV_8);
 set_adc_channel(0);
 delay_us(20);



 output_low(LED_PIN); // Set LED to Off

 while(1)
   {
    adc_result = read_adc();
     
    if(adc_result !=new && new >= val) {
       output_high(LED_PIN);
       delay_ms(100);
       adc_result=new; }
   }

 }
temtronic



Joined: 01 Jul 2010
Posts: 9202
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Apr 18, 2015 3:46 pm     Reply with quote

Basic trouble shooting 101

What is the output of the sensor ? Maybe it's busted ??

Normally you'd 'simulate' the sensor with a pot and test with it, recording the Vin to ADC versus the raw 10 bit results. That way you KNOW the PIC and your program are correct.

Also when posting, you should supply a 'link' to the sensor datasheet. There might be an impedance mismatch, power supply problem, etc. If we can quickly get the same datasheet you have, we can help faster !

Jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Apr 18, 2015 4:25 pm     Reply with quote

Is the line in bold below correct ?
Quote:
while(1)
{
adc_result = read_adc();

if(adc_result !=new && new >= val) {
output_high(LED_PIN);
delay_ms(100);
adc_result=new;
}
}

Shouldn't it really be as shown below ? Normally the reason for doing
this is to save the last value read, and compare it to the next reading on
the next pass through the loop.
Code:

while(1)
   {
    adc_result = read_adc();
     
    if((adc_result != new) && (new >= val))
      {
       output_high(LED_PIN);
       delay_ms(100);
       new = adc_result;   // *** It should be like this
      }
   }
fiasgardone



Joined: 19 Jan 2010
Posts: 71

View user's profile Send private message

PostPosted: Sat Apr 18, 2015 4:53 pm     Reply with quote

temtronic wrote:
Basic trouble shooting 101

What is the output of the sensor ? Maybe it's busted ??

Normally you'd 'simulate' the sensor with a pot and test with it, recording the Vin to ADC versus the raw 10 bit results. That way you KNOW the PIC and your program are correct.

Also when posting, you should supply a 'link' to the sensor datasheet. There might be an impedance mismatch, power supply problem, etc. If we can quickly get the same datasheet you have, we can help faster !

Jay


Here is the datasheet!

http://www.seeedstudio.com/depot/datasheet/MQ-2.pdf
fiasgardone



Joined: 19 Jan 2010
Posts: 71

View user's profile Send private message

PostPosted: Sat Apr 18, 2015 5:04 pm     Reply with quote

Hi!
The val variable must have a value to be compared with the variable adc_result reading AD converter, so that the LED only lights up when you have a value of anologica entry about 25% of gas entering the sensor!

What should be the correct value for this variable val?

Thank you very much
temtronic



Joined: 01 Jul 2010
Posts: 9202
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Apr 19, 2015 4:52 am     Reply with quote

In the comments of the OP, it says 'LM393' which is an opamp, so assuming you're using a premade board with the sensor feeding the LM393 AND that it's be configured to give 0 V out for 0 gas, 5 volts for 100% gas AND you're using the ADC in 10 bit mode THEN your 'trip point' of 25% would be 1024/4 = 256 bits, which is about 1.25 volts.

hth
Jay
fiasgardone



Joined: 19 Jan 2010
Posts: 71

View user's profile Send private message

PostPosted: Sun Apr 19, 2015 7:03 am     Reply with quote

temtronic wrote:
In the comments of the OP, it says 'LM393' which is an opamp, so assuming you're using a premade board with the sensor feeding the LM393 AND that it's be configured to give 0 V out for 0 gas, 5 volts for 100% gas AND you're using the ADC in 10 bit mode THEN your 'trip point' of 25% would be 1024/4 = 256 bits, which is about 1.25 volts.

hth
Jay



Hi! temtronic thanks for help !!

Yes, I am using a premade board with the sensor feeding the LM393 as pictured above .

Well, so the variable ( val) will have a value of 1.25V / 1024 = 0.001220 V/bit, so the variable( val) must be float.
The value of the variable ( val )will be compared with the (adc_result )variable and if the ADC value is greater than the value of the variable (val )lights the LED

The problem is I have done enough testing and does not work!
Our entire help is welcome

Thank you very much

Code:
#include <18F458.H>
 #device adc=10
 #fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
 #use delay(clock=20000000)


 #define LED_PIN  PIN_B0
 
 
 //==========================================
 void main()
 {
 int16 new,adc_result;
  float val=0.001220;
 setup_adc_ports(AN0);
 setup_adc(ADC_CLOCK_DIV_8);
 set_adc_channel(0);
 delay_us(20);



 output_low(LED_PIN); // Set LED to Off

 while(1)
   {
    adc_result = read_adc();
     
    if(adc_result !=new && new >= val) {
       output_high(LED_PIN);
       delay_ms(100);
       adc_result=new; }
   }

 }
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sun Apr 19, 2015 9:59 am     Reply with quote

You're STILL making most of the errors outlined in the responses you've had.

Simply saying "I have done enough testing and does not work! " tells us nothing.
What do you see happening?
Make up a link to a PC with RS232.
Then you can SEE what's going on.

Mike
dyeatman



Joined: 06 Sep 2003
Posts: 1931
Location: Norman, OK

View user's profile Send private message

PostPosted: Sun Apr 19, 2015 12:09 pm     Reply with quote

Quote:
Well, so the variable ( val) will have a value of 1.25V / 1024 = 0.001220 V/bit, so the variable( val) must be float.

No, it does not have to be and should NOT be a float.
The ADC puts out a 10 bit INTEGER value, not a float!

As temtronic tried to tell you the value for val needs to be 256.

In addition to that Mike pointed out you are still making the
fundamental mistakes pointed earlier in the post.

1. You never turn the LED off after the delay
2. You are overwriting the adc_result value and new is never set properly.

If you are not going to do what we suggest then we are wasting our time...
_________________
Google and Forum Search are some of your best tools!!!!
fiasgardone



Joined: 19 Jan 2010
Posts: 71

View user's profile Send private message

PostPosted: Sun Apr 19, 2015 2:20 pm     Reply with quote

dyeatman wrote:
If you are not going to do what we suggest then we are wasting our time...


If I'm here to ask for help is because I am not able to deal alone this AD problem, and I always try to understand and do what is said here, and never ignore the help of you!

I do not want the code ready, wanted to help me resolve the errors.

I'm doing the test physically in Breadboard, and I'm not doing any simulator! the tests inject a little gas on the sensor, note that the LED does not light. Well made some modifications to the code, and without work.

Thank you very much

Code:
 #include <18F458.H>
 #device adc=10
 #fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
 #use delay(clock=20000000)


 #define LED_PIN  PIN_B0
 
 
 //==========================================
 void main()
 {
 int16 adc_result,new;
 int16 val= 256;

 setup_adc_ports(AN0);
 setup_adc(ADC_CLOCK_DIV_8);
 set_adc_channel(0);
 delay_us(20);

 
 new=adc_result;

 //output_low(LED_PIN); // Set LED to Off

while(1)
   {
    adc_result = read_adc();
     
    if((adc_result != new) && (new >= val))
      {
       output_high(LED_PIN);
       delay_ms(100);
       new = adc_result;   
      }
   }

 }
temtronic



Joined: 01 Jul 2010
Posts: 9202
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Apr 19, 2015 3:03 pm     Reply with quote

You should really try running your program and using a 10K pot as the 'sensor'. That way you know what the input to the ADC is, unless you have a DVM on the input.

Right now you have no way of knowing whether it's your program or a bad sensor module. This is why I'm suggesting using a pot. It is easy to confirm as a 'known' input.


Jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Apr 19, 2015 3:05 pm     Reply with quote

You need to find out if your ADC output is good. Do this by displaying the
output on a terminal window on your PC. Use an rs-232 serial port to
do this. If your PIC doesn't have a COM port connector, then get a
USB-to-serial adapter cable and use that. TeraTerm is a good terminal
window program.

Your connections would be like this, except you would use pins C6 and C7
on the PIC:
http://www.islavici.ro/cursuriold/conducere%20sist%20cu%20calculatorul/PICbook/7_chapter/44.gif

Test program for serial output of ADC results:
Code:

#include <18F458.H>
#device adc=10
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, UART1, ERRORS)

#define LED_PIN  PIN_B0
   
 //==========================================
void main()
{
int16 adc_result;

setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_DIV_32);
set_adc_channel(0);
delay_us(20);


while(1)
   {
    adc_result = read_adc();
    printf("%lx \n\r", adc_result);
    delay_ms(500);
   }

}
fiasgardone



Joined: 19 Jan 2010
Posts: 71

View user's profile Send private message

PostPosted: Mon Apr 20, 2015 10:09 am     Reply with quote

Hi!
Thank you all!

Well .... therefore I have to take the tests as anybody show!

After doing the test, I post the results here!

Thank you very much
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group