My MCP3008 did not send any value to processing!?

edited March 2018 in Raspberry PI

There are a few question that has been asked in this forum here that I've followed them already but both did not work with my Rpi.

Here are two from a few of them I have worked around but fail. (1), (2) I see the 2nd example from a site that seems like a legit Github of Processing team (?) is a very updated one but I still get only 0 from it. However, when run the python in terminal my sensors do work well.

I'm using Rpi 3 B and my code is exactly copy and paste from the 2nd. I've created a new folder in Rpi and have two file.pde one is a class and one is a main function (setup and draw).

So, what else should I do or any info that I should looking into to fix this problem?

Tagged:

Answers

  • Are you getting an error message, or is it just that you are getting 0 instead of values?

  • edited February 2018

    @Jeremydouglass Not at all! I only get 0. That's why I have no clue to debug or anything. I am not with my Rpi right now So I cannot screenshot/put my code in here. But, I am sure my code is exactly like the link (2). The data coming into Rpi for sure. I can see that through terminal. Also, the organization of my files in a folder also separate into two .pde with similar name like his. Moreover, I have already tried running only Processing code or Python through terminal at different time. In case this is like Arduino talking to Processing over a Serial port where only one can use the port. However, 0 is what I still got.

    hmm... I wonder if I have to "turn on" the sensor somehow before click play in Processing IDE? What should I do?

  • From your second link, it is not clear how you are sending the data, at least not from the second link above. To clarify, you are running Processing in your R-pi and then... how are you retrieving this data? Are you connecting a PC to your R-pi and using a terminal to read the serial port?

    Please provide the source code (MCVE).

    Kf

  • edited February 2018

    Hi @kfrajer,

    Here is how I test my sensor. I followed Adafruitinstruction. And, YES! I run processing IDE on Rpi. Here is my code and snapshots from Rpi monitor.

    import processing.io.*;
    MCP3008 adc;
    
    // see setup.png in the sketch folder for wiring details
    
    void setup() {
      //printArray(SPI.list());
      adc = new MCP3008(SPI.list()[0]);
    }
    
    void draw() {
      background(adc.getAnalog(0) * 255);
      fill(adc.getAnalog(1) * 255);
      println(adc.getAnalog(1) );
      ellipse(width/2, height/2, width * 0.75, width * 0.75);
    }
    
    import processing.io.SPI;
    
    // MCP3008 is a Analog-to-Digital converter using SPI
    // other than the MCP3001, this has 8 input channels
    // datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/21295d.pdf
    
    class MCP3008 extends SPI {
    
      MCP3008(String dev) {
        super(dev);
        settings(500000, SPI.MSBFIRST, SPI.MODE0);
      }
    
      float getAnalog(int channel) {
        if (channel < 0 ||  7 < channel) {
          System.err.println("The channel needs to be from 0 to 7");
          throw new IllegalArgumentException("Unexpected channel");
        }
        byte[] out = { 0, 0, 0 };
        // encode the channel number in the first byte
        out[0] = (byte)(0x18 | channel);
        byte[] in = transfer(out);
        int val = ((in[1] & 0x03) << 8) | (in[2] & 0xff);
        // val is between 0 and 1023
        return val/1023.0;
      }
    }
    

    The sensor connect to channel 0 and 1. 0 is a pot and 1 is FSR As you see in these photos

    When touch the FSR the channel 1 change its value.

    But in IDE, I only got 0

  • @TuangPing

    Not sure if I understand correctly: Are you saying that channel 1 works, but channel 0 doesn't? Or are you saying that both are not working in Processing IDE, but in some other environment (which one)?

  • edited February 2018

    Hi @gohai, both of the channel work fine with my sensor. But, they do not work with processing. I tried println(adc.getAnalog(1) ); and println(adc.getAnalog(0) ); And I get 0.

    What shown in pictures saying that sensors on 0 and 1 are not incorrect installed because terminal of Rpi can see the value that changed. However, I couldn't make IDE of processing see it.

  • @TuangPing How do you go about determining in the terminal that the ADC is working correctly?

  • @gohai. What do you mean by determining the ADC? If I understand correctly what you mean; I touched the sensor FSR and rotating the potentiometer and the value change! Although I do not know why the rest of the channels from 3-7 has random values.

  • @TuangPing What program or tool are you using in the terminal?

  • edited February 2018

    @gohai .

    Here is a link I follow from Adafruit I have mentioned ealier. However, I attach a link directly to github of that person the python code is loaded from.

    To add more detail, according to Adafruit, I went with Software SPI. But, thinking if I should try wire like hardware SPI.

  • Answer ✓

    @TuangPing Yes, Processing's IO library only supports the hardware SPI interface. See here: https://github.com/processing/processing/wiki/Raspberry-Pi#spi

  • @gohai, Hey! Thank you so much :D ! they all work now.

  • @TuangPing Can you comment on how did you solve your problem please?

    Kf

  • edited April 2018

    @kfrajer Sure no problem ;D

    Look into Adafruit link I provided earlier. There, u'll see they mention a software SPI and a hardware SPI. Like @gohai says, Processing accesses the data from using the hardware SPI not the software. Therefore, you have to connect your pins follow the hardware SPI instruction!

    On the Adafuit page, they do not explain this. However, in the link that @gohai posted, which I've selected as the answer, will tell you why.

  • @TuangPing Thank you.

    Kf

Sign In or Register to comment.