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!