mcp3008

417 views
Skip to first unread message

Chelaru Viorel

unread,
May 19, 2013, 5:34:28 AM5/19/13
to pi...@googlegroups.com
hi,
I'm new to pi4j, so excuse me if my questions are stupid.

I bought a sensor shield that has a MCP3008 chip on it. I want to read analog sensors connected to it.

1) can I use MCP23008GpioProvider ? because MCP3008GpioProvider does not exists.

2) the shield should be detected by i2cdetect ? because I have no address filled...every address is blank, so it doesn't see it. In MCP23008GpioProvider I have to provide the address binded in i2c ?


thanks very much for help,
Viorel

Pi4J

unread,
May 25, 2013, 5:02:26 PM5/25/13
to pi...@googlegroups.com
Hi Viorel,

No question is stupid in my book.

1) can I use MCP23008GpioProvider ? because MCP3008GpioProvider does not exists.

No, the MCP23008 only supports digital I/O whereas the MCP3008 is a 10 bit A/D converter to handle analog signals.  
Pi4J just recently added analog support using the ADS1115 chip in the latest sources:

2) the shield should be detected by i2cdetect ? because I have no address filled...every address is blank, so it doesn't see it. In MCP23008GpioProvider I have to provide the address binded in i2c ?
 
Check out the steps in this article to make sure your Pi is configured properly for I2C communication.


Thank You,
Robert 

Chelaru Viorel

unread,
May 25, 2013, 5:47:48 PM5/25/13
to pi...@googlegroups.com, pi...@googlegroups.com
Hi Robert,
thank you very much for the response.
I finally figured it out. I was making a confusion, that MCP3008 talks I2c but it talks SPI.
There are 2 ways of reading from the MCP3008.
1) connect though pins 18, 23,24, 25 of raspberry pi and do a bit manipulation(this was before there was a SPI interface on the raspberry pi)
2) connect the MCP directly to the SPI pins on the raspbery pi : clk, mosi, miso, ce01/02.

I've tried so far 1) option because I found the bit manipulation code in python and I translated it into java. Here it is if you're interested:
public class SensorTest {

   
public static int readadc(int adcnum, GpioPinDigitalOutput clockpin,
           
GpioPinDigitalOutput mosipin, GpioPinDigitalInput misopin,
           
GpioPinDigitalOutput cspin, GpioController gpio) {
       
if ((adcnum > 7) || (adcnum < 0))
           
return -1;

        cspin
.high();
        clockpin
.low();
        cspin
.low();

       
int commandout = adcnum;
        commandout
|= 0x18;
        commandout
<<= 3;
       
for (int i = 0; i < 5; i++) {
           
if ((commandout & 0x80) != 0) {
                mosipin
.high();
           
}

           
else {
                mosipin
.low();
           
}
            commandout
<<= 1;
            clockpin
.high();
            clockpin
.low();
       
}

       
int adcout = 0;
       
for (int i = 0; i < 12; i++) {
            clockpin
.high();
            clockpin
.low();
            adcout
<<= 1;
           
if (misopin.getState() == PinState.HIGH) {
                adcout
|= 0x1;
           
}

       
}

        cspin
.high();

        adcout
>>= 1;
       
return adcout;

   
}

   
public static void main(String[] args) throws InterruptedException,
           
IOException {

       
System.out.println("<--Pi4J--> GPIO Listen Example ... started.");

       
// create gpio controller
       
GpioController gpio = GpioFactory.getInstance();

       
GpioPinDigitalOutput SPICLK = gpio.provisionDigitalOutputPin(
               
RaspiPin.GPIO_01, "SPICLK", PinState.LOW);
       
GpioPinDigitalInput SPIMISO = gpio.provisionDigitalInputPin(
               
RaspiPin.GPIO_04, "SPIMISO", PinPullResistance.PULL_DOWN);
       
GpioPinDigitalOutput SPIMOSI = gpio.provisionDigitalOutputPin(
               
RaspiPin.GPIO_05, "SPIMOSI", PinState.LOW);
       
GpioPinDigitalOutput SPICS = gpio.provisionDigitalOutputPin(
               
RaspiPin.GPIO_06, "SPICS", PinState.LOW);


       
       
for (int i=0;i <= 20;i++) {
           
float value = readadc(0, SPICLK, SPIMOSI, SPIMISO, SPICS, gpio);
           
System.out.println("sensor value= " + value);
           
Thread.sleep(500);
       
}
       
       
        gpio
.shutdown();

   
}
}




I didn't have time to check option 2) because I had so much to do at work(long boring non-interesting stuff), but it should work (without the bit manipulation code) as in https://github.com/Pi4J/pi4j/blob/master/pi4j-example/src/main/java/SerialExample.java .
I'll try it after I free up.

have a nice day,
Viorel

Stefan Schildbach

unread,
Mar 17, 2014, 5:48:11 PM3/17/14
to pi...@googlegroups.com
Hey,

I know old threads should be left at rest, but my question refers exactly to the MCP3008 microchip discussed here. I'm relatively new to the PI and therefore to the Pi4J library.
I bought a MCP3008-I/P microchip and connected it to the PI.
Different pin setups as well as various guides had been tested:
On the first try, I did not connect channel 0 to anything and the expected result of nothing was fine.
Afterwards, I connected it with the 3.3V source of the pi (same is used for VDD and VREF) to simplify my setup. I expected a value close to 1023 - but I got a wide variety of useless numbers between 0 and 1023 while running the programs in a loop.

So the most important question for me is:
Does the code stated by Chelaru Viorel work properly (I even get values above 1023 there) respectively is my chip broken or do I miss a key part in communicating with the microchip?
Do I need to add sleep times between triggering a clock flank or are there any other known problems?

I would be really thankful if someone with experience / knowledge would take a look at this.

Thanks in advance
Stefan

P.S.: My code sample for communicating:
public static void main(String[] args) throws Exception
{
GpioController gpioController = GpioFactory.getInstance();
GpioPinDigitalOutput clock = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_01, PinState.LOW);
GpioPinDigitalInput digitalOut = gpioController.provisionDigitalInputPin(RaspiPin.GPIO_04, PinPullResistance.OFF);
GpioPinDigitalOutput digitalIn = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_05, PinState.LOW);
GpioPinDigitalOutput select = gpioController.provisionDigitalOutputPin(RaspiPin.GPIO_06, PinState.LOW);
clock.setShutdownOptions(true, PinState.LOW);
digitalOut.setShutdownOptions(true);
digitalIn.setShutdownOptions(true, PinState.LOW);
select.setShutdownOptions(true, PinState.LOW);
for (int i = 0; i < 10; i++)
{
portCommunication2(select, clock, digitalIn, digitalOut);
Thread.sleep(1000);
}
gpioController.shutdown();
}

private static void portCommunication2(GpioPinDigitalOutput select, GpioPinDigitalOutput clock, GpioPinDigitalOutput digitalIn, GpioPinDigitalInput digitalOut)
{
long transmittedData = 0;
long recievedData = 0;
int position = 24;
// Initiate communication
select.high();
select.low();
// Configuration
boolean[] configuration = new boolean[]
false, false, false, false, false, false, false, true, // Start byte
true, // single-ended (true) or differential (false)
false, false, false // Channel 0-7
// Don't care data / NOP
, false, false, false, false,
false, false, false, false, false, false, false, false
};
for (boolean b : configuration)
{
position--;
digitalIn.setState(b);
transmittedData |= ((digitalIn.isHigh() ? 1 : 0) << (position));
clock.high();
clock.low();
recievedData |= ((digitalOut.isHigh() ? 1 : 0) << (position));
}
// Format data output
System.out.println(">");
System.out.println(transmittedData);
System.out.println(recievedData);
// printLong(transmittedData, false);
// printLong(recievedData, true);
// System.out.println();
}
Reply all
Reply to author
Forward
Message has been deleted
0 new messages