0

I'm in the midst of a school project and having trouble with reading ADC values from an MCP3004. I have it done in two different ways but one of them won't work.

Using wiringPi.h and mcp3004.h, all written in C.

//Including necessary libraries 
#include <wiringPi.h>
#include <wiringPiSPI.h>
#include <wiringPiI2C.h>
#include <wiringSerial.h>
#include <mcp3004.h>           
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include <errno.h>

//Definitions used for the MCP3008  
#define BASE 100        
#define SPI_CHANNEL 0 

//Function to read ADC value from specified channel on the MCP3008 
//Call the function, input desired channel number, returns ADC as a float 
float reading_Channel(int channel_number)
{
    int channel_read, channel_out; 

    channel_read = analogRead(BASE + channel_number);
    channel_out = channel_read;

    return channel_out; 
}

int main(void)
{
/*---------------------------------------------------------------------------------------------------------------------------------*/       
    //MAIN SETUP HERE
/*---------------------------------------------------------------------------------------------------------------------------------*/   
    //WiringPi setup, different than wiringPiSetup() as this uses actual Broadcom pin numbers instead of virtual pin remapping.
    wiringPiSetupGpio();

    //Initialize SPI channel with the RPi for the MCP3008.
    wiringPiSPISetup(1, 5*1000*1000);   //wiringPiSPISetup(int channel, int speed) 
    delay(50);

    //Initialize I2C channel with the RPi for the MAX517.
    int fd_1;
    if ((fd_1 = wiringPiI2CSetup(0x2c)) <0) //wiringPiI2CSetup(device ID) 
    {
        fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno));
        return 1;
    }

    //Checks if WiringPi is initialized.
    if (wiringPiSetup () == -1)
        {
            fprintf (stdout, "Unable to start wiringPi: %s\n", strerror (errno));
            return 1;
        }

    //Checks if Serial communications is initialized.
    int fd;
    if ((fd = serialOpen ("/dev/ttyUSB0", 115200)) <0) 
        {
        fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno));
        return 1;
        }

    //WiringPi has a built-in functionality with the MCP300X ADC. 
    mcp3004Setup (BASE, SPI_CHANNEL);

    //Declaring MCP 3008 input variables pertaining to the temperature sensor.  
    int temp;

    //Declaring MCP 3008 input variables pertaining to the potentiometer.  
    int setpoint; 
/*---------------------------------------------------------------------------------------------------------------------------------*/   
    //BULK OF WORK HERE
/*---------------------------------------------------------------------------------------------------------------------------------*/       
    //Looping the math/code required for readings
        while(1)
        {   
            //Math required for temperature output from ADC.
            temp = reading_Channel(0); //reads ADC value from channel 0         
            temp = ((temp/1024)*3.3); //Converts ADC value to Voltage.
            temp = ((temp*1000) - 500) / 10; //Converts Voltage to temperature.

            //Serial print to USART here. 
            delay(1000);
            serialPrintf (fd, temp); //Emulates the system printf function to the serial device.

            //Math required for potentiometer output from ADC.
            setpoint = reading_Channel(1); //reads ADC value from channel 1
            setpoint = ((setpoint/1024)*5); //Converts ADC value to Voltage.
            setpoint = (4 * setpoint + 15); //Linear conversion from Voltage to temperature range (15° - 35°). 

            //Serial print to USART here. 
            delay(1000);
            serialPrintf (fd, setpoint); //Emulates the system printf function to the serial device.

            serialFlush(fd); //Discards all data received, or waiting to be send down the given device.
        }       
}

When running, the math outputs as if the ADC values were 0.

Wondering why the function above wont continuously acquire the ADC value? Any help would be greatly appreciated!

  • I can't see a question. If you have a working version why aren't you using it? – joan Apr 15 at 11:12
  • @joan Just wondering why the function wouldn't have the same operation. As if I had multiple channels instead of 2 I wouldn't want to define so many variables and repeat existing code. – WetOnTheWater Apr 15 at 11:32
  • 1
    Why don't you post a complete program? I'm not going to guess why a part of a program doesn't do what you expect. – joan Apr 15 at 12:12
  • @joan Sorry about that, edited to post the entire program now. – WetOnTheWater Apr 15 at 12:41
1

This can't be right:

int temp;
...
temp = ((temp/1024)*3.3);

The rules of integer division say that any value below 1024 divided by 1024 is zero.

Try using floats for the math.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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