gmc
Posts: 123
Joined: Fri Mar 09, 2012 11:31 am
Location: Cheshire, UK
Contact: Website

C code for MCP3008

Sat Aug 31, 2013 3:50 pm

Thought I'd just post this here - took me ages to find a working C library, but this one works great.

Hopefully it will help someone else out.

http://gpcf.eu/projects/embedded/adc/

I originally used a c++ one but needed to go back to C.

If you want a C++ library this works as well. (I could never get the Adafruit one working)

http://hertaville.com/2013/07/24/interf ... i-using-c/

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: C code for MCP3008

Mon Sep 02, 2013 2:26 pm

gmc wrote:Thought I'd just post this here - took me ages to find a working C library, but this one works great.

Hopefully it will help someone else out.

http://gpcf.eu/projects/embedded/adc/

I originally used a c++ one but needed to go back to C.

If you want a C++ library this works as well. (I could never get the Adafruit one working)

http://hertaville.com/2013/07/24/interf ... i-using-c/
wiringPi has a built-in for this chip:

Code: Select all


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

...

#define BASE 100
#define SPI_CHAN 0

  wiringPiSetup() ;
  mcp3004Setup (BASE, SPI_CHAN) ; // 3004 and 3008 are the same 4/8 channels

  for chan = 0 ; chan < 8 ; ++chan)
    x = analogRead (BASE + chan) ;
... etc.
-Gordon
--
Gordons projects: https://projects.drogon.net/

gmc
Posts: 123
Joined: Fri Mar 09, 2012 11:31 am
Location: Cheshire, UK
Contact: Website

Re: C code for MCP3008

Mon Sep 02, 2013 2:35 pm

wow - even easier.

Time to recode!

Thanks.

gmc
Posts: 123
Joined: Fri Mar 09, 2012 11:31 am
Location: Cheshire, UK
Contact: Website

Re: C code for MCP3008

Fri Sep 13, 2013 4:08 pm

Trying to get the wiringpi working and am struggling.

In the above code what variable do the following represent:

Code: Select all

#define BASE 100
#define SPI_CHAN 0

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: C code for MCP3008

Fri Sep 13, 2013 4:15 pm

gmc wrote:Trying to get the wiringpi working and am struggling.

In the above code what variable do the following represent:

Code: Select all

#define BASE 100
#define SPI_CHAN 0

In this instance, the SPI_CHAN is fairly obvious - it's the SPI channel (0 or 1) that the peripheral is connected to. The BASE is the new pin base for the chip. So the inputs to the chip are on pins 100 through 107. wiringPi creates these new pins for you when you call the setup function.

value = analogRead (100) ;

will read the first port on the chip.

-Gordon
--
Gordons projects: https://projects.drogon.net/

gmc
Posts: 123
Joined: Fri Mar 09, 2012 11:31 am
Location: Cheshire, UK
Contact: Website

Re: C code for MCP3008

Fri Sep 13, 2013 5:46 pm

Odd - tried that. Its not reading the values. I know the setup is correct as I have another c which uses different libraties and its retrieving values with it.

Anything glaringly obvious?

Code: Select all

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


#define BASE 200
#define SPI_CHAN 0

int main(void)

{
  int chan;
  int x;
printf ("Raspberry Pi wiringPi test program\n") ;

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

  mcp3004Setup (BASE, SPI_CHAN); // 3004 and 3008 are the same 4/8 channels

  for (chan = 0 ; chan < 8 ; ++chan) {
    x = analogRead (BASE + chan) ;
  printf("%d\n", x);
    }
   return 0 ;
}

05hicklingmr
Posts: 8
Joined: Mon Nov 04, 2013 10:22 am

Re: C code for MCP3008

Tue Dec 17, 2013 11:16 am

gmc wrote:Odd - tried that. Its not reading the values. I know the setup is correct as I have another c which uses different libraties and its retrieving values with it.

Anything glaringly obvious?

Code: Select all

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


#define BASE 200
#define SPI_CHAN 0

int main(void)

{
  int chan;
  int x;
printf ("Raspberry Pi wiringPi test program\n") ;

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

  mcp3004Setup (BASE, SPI_CHAN); // 3004 and 3008 are the same 4/8 channels

  for (chan = 0 ; chan < 8 ; ++chan) {
    x = analogRead (BASE + chan) ;
  printf("%d\n", x);
    }
   return 0 ;
}

hey! did you get anywhere with this... ui am also struggling with analogread on the wiringpi library...

gmc
Posts: 123
Joined: Fri Mar 09, 2012 11:31 am
Location: Cheshire, UK
Contact: Website

Re: C code for MCP3008

Tue Dec 17, 2013 11:21 am

Nope - unfortunately never got the code working. I'm still at a loss as to why it doesn't work.

joliverl
Posts: 6
Joined: Mon Dec 09, 2013 8:55 am

Re: C code for MCP3008

Tue Dec 17, 2013 1:55 pm

did you try to run:

Code: Select all

gpio load spi
gpio -x mcp3004:100:0 aread 100
This will at least give you the assurance that the wiring is correct. You need to have wiringpi installed.

J

joliverl
Posts: 6
Joined: Mon Dec 09, 2013 8:55 am

Re: C code for MCP3008

Tue Dec 17, 2013 9:16 pm

check this post for a working solution.
http://www.raspberrypi.org/phpBB3/viewt ... 05#p470705

martid95
Posts: 19
Joined: Fri Jun 21, 2013 10:52 am

Re: C code for MCP3008

Thu Mar 13, 2014 9:58 am

Thought I'd just post this here - took me ages to find a working C library, but this one works great.

Hopefully it will help someone else out.

http://gpcf.eu/projects/embedded/adc/
Hi to everyone :) I'm having the same problem as you using MCP3008. I tried to use "mcp3008.h" from gpcf.eu but it doesn't work. I get exit status 139 on gpio_init(clock, "out"). Is there someone who can help me?
Otherwise I found a library for MCP3004 on wiringPi, someone can tell me what should I modify on that library to make it work with 3008??


I'm getting crazy.... thanks in advance ( and sorry for my English)

thomasemil
Posts: 2
Joined: Wed May 31, 2017 12:13 pm

Re: C code for MCP3008

Wed May 31, 2017 12:15 pm

mcp3004 works for me

Code: Select all

// includes the wiringPI_api
#include <wiringPi.h>
// includes C standard library 
#include <stdio.h>
//BASE is a new pin base for the chip of the analog pins
#define BASE 100
// chose the SPI_channel
#define SPI_CHAN 0
#include "mcp3004.h"

Int main()
{
	// starts the wiringPi_api
	If (wiringPiSetup() == -1)
	{
	return 1;
	}
/*********all*of*this*is *for*the*mcp300X************/
	int chan;
	int mcp_answer;
	//if you use a mcp3008 write canche it in the setup
	mcp3004Setup(BASE, SPI_CHAN);
/************************************************/
	while (1)
{
// with chan can you set what analog channels you want to read
                   for ( chan = 0; chan < 4 ;chan++) 
                  	 {
                  	 mcp_answer = analogRead( BASE + chan);
		 printf(“%d”, mcp_answer);
                  	 }
// return 0 breaks the loop
return 0;
}
}

calvinjarrod
Posts: 1
Joined: Mon Jun 26, 2017 4:32 pm

Re: C code for MCP3008

Mon Jun 26, 2017 4:39 pm

This code worked for me. I had a little trouble until I connected CE0 on the RPi to SC on the MCP3008 chip. I've read through the SPI library info on Gordon's website. How would you change the code to read from two channels on the chip?

thomasemil
Posts: 2
Joined: Wed May 31, 2017 12:13 pm

Re: C code for MCP3008

Mon Jun 26, 2017 8:34 pm

calvinjarrod wrote: How would you change the code to read from two channels on the chip?
The code should work for multiple channels , i didn't tried because i only needed one, but i got 4 different values for sure. So i don't see a problem there.
But i will try the other channels if you want to, just post a reply and let me know.

Sry i dont know if i got you question right.
Just edit chan <2 and you got your two channels.
or what do you want ?

aboutPi
Posts: 5
Joined: Mon Nov 13, 2017 5:41 am

Re: C code for MCP3008

Mon Nov 13, 2017 5:52 am

I have tried to use wiringPi for a MCP3008 and Potentiometer like abouve, but I did not get any value.

Code: Select all

...
wiringPiSetupGpio ();
...
mcp3004Setup(this->MCP3008_BASE, this->MCP3008_SPI_CHAN) ; // 3004 and 3008 are the same 4/8 channels
...
int value = analogRead(this->MCP3008_BASE);
...
Otherwise using a Python Script everything works well. These are my Pin Settings:

Code: Select all

# Konfiguration Eingangskanal und GPIOs
CH = 0  # Analog/Digital-Channel
CLK     = 18 # Clock
DIN     = 24 # Digital in
DOUT    = 23  # Digital out
CS      = 25  # Chip-Select

# Pin-Programmierung
GPIO.setup(CLK, GPIO.OUT)
GPIO.setup(DIN, GPIO.OUT)
GPIO.setup(DOUT, GPIO.IN)
GPIO.setup(CS,   GPIO.OUT)
Don't know how to solve the problem. Any idea?

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: C code for MCP3008

Mon Nov 13, 2017 11:05 am

aboutPi wrote:
Mon Nov 13, 2017 5:52 am
I have tried to use wiringPi for a MCP3008 and Potentiometer like abouve, but I did not get any value.

Code: Select all

...
wiringPiSetupGpio ();
...
mcp3004Setup(this->MCP3008_BASE, this->MCP3008_SPI_CHAN) ; // 3004 and 3008 are the same 4/8 channels
...
int value = analogRead(this->MCP3008_BASE);
...
Don't know how to solve the problem. Any idea?
Not knowing what this->base and this->chan is, then I've no idea.

If this work at the command-line level:

Code: Select all

  gpio -xmcp3004:777:0 aread 777
then this will work in code:

Code: Select all

  mcp3004Setup (777, 0) ;
  value = analogRead (777 + 0) ; // Reads channel 0
  value2 = analogRead (777+7) ; // Reads channel 7
-Gordon
--
Gordons projects: https://projects.drogon.net/

aboutPi
Posts: 5
Joined: Mon Nov 13, 2017 5:41 am

Re: C code for MCP3008

Mon Nov 13, 2017 4:24 pm

Hello Gordon,
thank you for your reply.

These are the values of the variables:

Code: Select all

class PotController
{
	private:
		//BASE is a new pin base for the chip of the analog pins	
		const int MCP3008_BASE	= 100; 
		// chose the SPI_channel
		const int MCP3008_SPI_CHAN	= 0; 

Running the command gpio -x mcp3004:777:0 I get 0 as result.

Any idea?

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: C code for MCP3008

Mon Nov 13, 2017 4:34 pm

aboutPi wrote:
Mon Nov 13, 2017 4:24 pm
Hello Gordon,
thank you for your reply.

These are the values of the variables:

Code: Select all

class PotController
{
	private:
		//BASE is a new pin base for the chip of the analog pins	
		const int MCP3008_BASE	= 100; 
		// chose the SPI_channel
		const int MCP3008_SPI_CHAN	= 0; 
I tried the command but I received the following error:

Code: Select all

gpio: Unknown command: -xmcp3004:777:0.
Any idea?
Needs a space between the x and the m

The base & channels look OK though - you do need the spi kernel device loaded for wiringPi though. Make sure they're loaded using the raspi-config program.

-Gordon
--
Gordons projects: https://projects.drogon.net/

aboutPi
Posts: 5
Joined: Mon Nov 13, 2017 5:41 am

Re: C code for MCP3008

Mon Nov 13, 2017 4:43 pm

I run raspi-config as sudo and enabled SPI and disabled I2C. Then I tried the command gpio load spi, but got these error:

Code: Select all

gpio: Unable to load/unload modules as this Pi has the device tree enabled.
  You need to run the raspi-config program (as root) and select the
  modules (SPI or I2C) that you wish to load/unload there and reboot.
  There is more information here:
      https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=97314

I tried the gpio -x mcp3004 command but the result was 0.

I will have a look, how to enable the spi and load the spi driver on my raspberry pi 3.

User avatar
[email protected]
Posts: 2020
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: C code for MCP3008

Mon Nov 13, 2017 4:49 pm

aboutPi wrote:
Mon Nov 13, 2017 4:43 pm
I run raspi-config as sudo and enabled SPI and disabled I2C. Then I tried the command gpio load spi, but got these error:

Code: Select all

gpio: Unable to load/unload modules as this Pi has the device tree enabled.
  You need to run the raspi-config program (as root) and select the
  modules (SPI or I2C) that you wish to load/unload there and reboot.
  There is more information here:
      https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=97314

I tried the gpio -x mcp3004 command but the result was 0.

I will have a look, how to enable the spi and load the spi driver on my raspberry pi 3.
That "error" means it's 2017 and you should be using the device tree. That's why I said to use raspi-config. The only people who try to use the gpio command in that way are a small number of my customers who're still running legacy systems. No ordinary Pi user needs that these days. Don't do it.

-Gordon
--
Gordons projects: https://projects.drogon.net/

aboutPi
Posts: 5
Joined: Mon Nov 13, 2017 5:41 am

Re: C code for MCP3008

Wed Dec 06, 2017 12:28 pm

I got some problem reading the Channel 1 (second input) on the mcp3008. Using the following Code on my SoftPot Membrane Potentiometer
(nothing connected to first input channel of the mcp3008)

int MCP3008_BASE = 100;
int MCP3008_SPI_CHAN = 0;

wiringPiSetupSPISetup(MCP3008_SPI_CHAN, 500000);
...
mcp3004Setup(MCP3008_BASE, MCP3008_SPI_CHAN) ;
...
int value = analogRead(MCP3008_BASE + 1);

I'm getting permanent values between 946 and 949. If I press the SoftPot, connected on the second Input Pin of the mcp3008, I get a different Value. But on the complete SoftPot range I will get nearly the same value.

If I connect the SoftPot on the first input pin of the mcp3008 and using

int value = analogRead(MCP3008_BASE);

There permanent value is 0. The range on the SoftPot is now 0 to 980.

Any hint, how I could read nearly the same Range on the second input pin of the mcp3008?

Best regards

aboutPi
Posts: 5
Joined: Mon Nov 13, 2017 5:41 am

Re: C code for MCP3008

Wed Dec 06, 2017 1:31 pm

Now it works. Must haven been something on the wire connections.
Sorry

domr001
Posts: 2
Joined: Thu Jan 25, 2018 1:58 pm

Re: C code for MCP3008

Thu Jan 25, 2018 2:18 pm

After a lot of hair pullingout, and and some great help from Gordon (thank you again) I have a simple complete AD program and the circuit wiring

This is the code ...

Code: Select all

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

#define SPI_CHAN 0
#define MY_PIN 12345

int main(void)
{
int x[8],i;
float v[8];
float k = 3.3/1024;

  wiringPiSetup() ;
  mcp3004Setup (MY_PIN, SPI_CHAN); // 3004 and 3008 are the same 4/8 channels

	for (i=0;i<8;i++)
	{
		x[i] = analogRead (MY_PIN + i ) ;
		v[i] = k * (float)(x[i]);
	}
	printf("AD ch: = %4d %4d %4d %4d %4d %4d %4d %4d \n",
			x[0],x[1],x[2],x[3],x[4],x[5],x[6],x[7]);
	printf("AD vd: = %2.2f %2.2f %2.2f %2.2f %2.2f %2.2f %2.2f %2.2f\n",
			v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7]);

	return 0;
}
This is the wiring for a MCP3008 (MCP column is IC pin no.) ...

Code: Select all

 +-----+-----+---------+------+---+---Pi 3---+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | MCP  | V | Physical | V | MCP  | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |  10 |  12 |    MOSI |  11  | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO |  12  | 0 | 21 || 22 | 1 |      | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK |  13  | 0 | 23 || 24 | 1 | 10   | CE0     | 10  | 8   |
 +-----+-----+---------+------+---+---Pi 3---+---+------+---------+-----+-----+
Also Pin 15, 16 --> 3.3v
Pin 9, 14 --> GND
Pin 1..8 --> Analog signals to be measured

Return to “C/C++”