2

I am using wiringPi and C. I have a flame sensor and a MCP3004 ADC. Here is the program that I wrote, but I can't figure out why it won't read the analog values from the flame sensor.

#include <wiringPi.h>
#include <mcp3004.h>
#include <stdio.h>
#include <stdlib.h>

#define BASE 200
#define SPI_CHAN 0

int main(void) {

int channel;
int x;

if (wiringPiSetup () == -1)
    exit(1);

mcp3004Setup(BASE, SPI_CHAN);
for (channel = 0; channel<8; ++channel) {
    x=analogRead(BASE+channel);
    printf("%d\n",x);
    delay(1000);
}
return 0;
}

My program runs but it just outputs 0s. The wiring is correct and the sensors and MCP3004 work fine, I tested it with a python program and it gets the job done.

So the only problem is in my C code.

  • 3
    Well an MCP3004 only has 4 channels (0-3). I'd expect erroneous results for non-existent channels 4-7. As a general principle have you checked the status returned by those functions you call which return a status? Perhaps one is returning you a status indicating an error which you are ignoring. – joan May 17 '17 at 8:35
  • 1
    To further the above comment, looking at the source, mcp3004Setup() will return false on error and true on success, so perform a check there... for example: if ! mcp3004Setup(...){exit(1);} – stevieb May 17 '17 at 12:30
  • thanks for replying guys.. So basically what happened now is that for some reason my c program works and the python program that I used to check if I connected the mcp and fire sensors correctly, doesn't. I swear to god i didn't touch anything no idea what happened. Fortunately this program works fine. Thanks so much for fast replying and interest though guys. I will also post here another code that works for mcp3008 (a little bit more advanced) for future coders/developers :) shaunsbennett.com/piblog/?p=266 – paulaxa1 May 17 '17 at 16:24
0

The link here may help. They have the wiring shown pretty clearly and example programs for Python and C users.

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