Using MCP3002 for converting an analog signal from a light sensor. Advice needed

Hello,

I am trying to understand how to use Analog to Digital Converter MCP3002.
I have
ArduinoMega2560,
Analog to Digital Converter MCP3002 (datasheet)
and a light sensor.
Aim: to get a digital signal on an Arduino digital pin.

I am a beginner therefore the information I found is a little bit confusing to me. There is a topic
topic about MCP3002. It seems that the easiest is to use the SPI library.

This is a code

#include <SPI.h>
 
const int slaveSelect = 10; //Slave Select
const byte READ = 0x60; //Channel 0 Read, MSBF
byte MSB, LSB;
word ChannelA;
 
void setup()
{
  // Initialize serial port for com to host
  Serial.begin (9600);
  
  pinMode(slaveSelect, OUTPUT); //Sets slave pin as output
 
  SPI.begin(); //Starts SPI
  SPI.setBitOrder(MSBFIRST); //Sets MSB first
}
 
void loop()
{
  digitalWrite(slaveSelect,LOW);
  MSB = SPI.transfer(READ); //Pulls in the MSB
  LSB = SPI.transfer(0); //Pulls in the LSB
 
  ChannelA = word(MSB,LSB); //combines the to bytes into a word
  digitalWrite(slaveSelect,HIGH);
  
  Serial.println(ChannelA, DEC);
}

schematic is attached.

The problem is: it prints just 0. To be honest I do not really understand how SPI works. I am trying to figure it out.

I would appreciate any help.

Hi, can I ask why you are not using the 2560's own A to D, 10bit converter?
Also have you got the correct pins needed for SPI?

Tom...... :slight_smile:

I don't want to use 2560's ADC because I plan to connect other sensors and reading should be as mush simultaneous as possible. As I understand 2560 analogue inputs use common ADC thus most likely the signal from one sensor will influence the others.
I am not experienced ee therefore reasonable solution for me will be to use external ADC.

This is the header from the source code I found.

// MCP3002 - Example code using Arduino UNO.
// March 17,2013
// Wiring
//    Pin 10 to /CS
//    Pin 11 to Din
//    Pin 12 to Dout
//    Pin 13 to CLK

The picture of the mcp3002 pins I cut from the datasheet and attached. I checked schematic and it seems ok. May be I don't see something.

mcp3002.png

schematic is attached.

No it is not. That is a physical layout diagram and about as useful as a chocolate tea pot.

If you want us to check if that circuit is correct you must post a schematic.

I don't want to use 2560's ADC because I plan to connect other sensors and reading should be as mush simultaneous as possible.

That is just silly, it takes time to take any reading you gain nothing by this.

As I understand 2560 analogue inputs use common ADC

Yes.

thus most likely the signal from one sensor will influence the others.

Rubbish.

Hi, that header is for a UNO, not a 2560 which has a different pinout.

As Mike has said, no matters how you use your AtoD it will always take time to do the AtoD conversion.
How fasts do you want to do your AtoD, what is the application.
SPI comms will probably add a delay to the process, the 3002 only has two channels and ONE AtoD converter.

Please read the 2560 pinout/specs.

Tom...... :slight_smile:

Hi, thank you very much Tom!
You were right the mega2560s pins were wrong. The correct and incorrect schematics are attached.
The datasheet says: mcp3002 has 200 ksps max sampling rate at Vdd=5V.

My project is basically a sound source detector, which should detect a sound source (for example in the backyard) and then depending on the time of a day direct the light on the sound source. Sound source detector needs to read an array of microphones simultaneously. The first thing I tried was reading an analogue signal from several microphone breakout boards, but results were confusing. Then I recognised that even if I leave only one mic on A0, for example, I will get almost the same signal on A1, A2 and so on just by running:

analogRead(A0);
analogRead(A1);
analogRead(A2);

So, may be I move in a wrong direction. If you have some advice how to get simultaneous sound signal on the board I would appreciate it.

Thank you Mike as well. You are absolutely right, schematic is much easier to understand, now I will use it rather then a physical layout diagram.

Hi, with only one AtoD you will never be able to read all microphones at once.
You can scan and read them, if it takes 250mS to do all of them, then that should be fast enough, then check the highest amplitude input and work out the corresponding direction.

I think you are over thinking the project, scanning the microphones should be fast enough.

Before you go out and buy a heap of AtoD chips, just try it with all the analog inputs of the 2560.
The object you want to detect how fast will it be going?

Tom...... :slight_smile:

Then I recognised that even if I leave only one mic on A0, for example, I will get almost the same signal on A1, A2 and so on just by running: ....

You will get the same reading if you have nothing attached to the other pins. The pins are floating and are not charging the sample and hold capacitor on the front of the internal A/D.
The output impedance feeding into an analogue pin should be 10K or less, then there will be no cross talk.

then check the highest amplitude input and work out the corresponding direction.

Tom - I think direction finding does not work that way, basically it is the phase difference between the signals that give you the direction, and you only need two microphones.

Hi, Mike, I think the OP wants to use an array of microphones, but phase detection would be better but not necessarily easier to implement. Depends on the noise that is being detected.

Tom......... :slight_smile:

Indeed, the idea was to use the phase difference. It is probably harder to implement, nevertheless I will give it a try.

Before you go out and buy a heap of AtoD chips...

too late ))) But it was worth it. Now I learned a little bit about SPI lib and how to switch between different ADCs. I will definitely find an application for that.

The object you want to detect how fast will it be going?

It may not move at all. Basically the device should react on any recognizable sound (may be I should filter incoming signal to separate when an action needed)

You will get the same reading if you have nothing attached to the other pins. The pins are floating and are not charging the sample and hold capacitor on the front of the internal A/D.
The output impedance feeding into an analogue pin should be 10K or less, then there will be no cross talk.

Thanks for the explanation! I definitely will try to reduce an impedance of the mics and use the built in ADC.