would really appreciate it if someone could help me with this, I have a MIKROE NO2 2 click sensor which I have attached the product details below. So far I have been unable to create or find a code compatible with an Arduino Uno and I would appreciate it if someone could help me out! Thank you
Reading the datasheet it seems to be rather straightforward to read the sensor.
It connects to the SPI bus (but only MISO (=SDO), SCK and SS, no MOSI connection).
Conversion starts the moment the SS line is pulled low, and it seems the measurement is available immediately. Read 16 bits, use 12 of them (see page 21 of the datasheet), and you have your value.
Release the SS line (take it high) when done, and before taking a new measurement.
That should be all there is to it. Something like this should do:
digitalWrite(SS, LOW); // Initiate conversion & communication with the chip.
uint16_t data = SPI.transfer16(); // Read 16 bits from the SPI bus.
uint16_t sensorReading = (data > 1) & 0x0FFF; // Get the relevant bits.
digitalWrite(SS, HIGH); // Release the chip.
I omitted the SPI.begin() and any SPI settings you may need, see the SPI reference for details on this. The chip can do no more than 800 kHz (at 3.3V) or 1.6 MHz (at 5V) which are both lower than the default 4 MHz of an Uno, so you have to set this speed to something the chip can handle.
Of course this is not tested, it's just based on what I gleaned from the datasheet. Do study it more carefully than I did to confirm it's correct. I'm surprised that the conversion data is seemingly instantly available when the communication is initiated, the first two SPI clock ticks are apparently all the time it needs to do the conversion.
I use the MCP3202 ADC (a dual channel version of the MCP3201, used with the NO2 sensor) for a different type of sensor.
Here is PIC C code for reading the MCP3202. With perhaps minor modifications (e.g. no control byte need be sent) it should work with the MCP3201. It is easy to translate the calls to Arduino C/C++.
#define MCP3202_CLK PIN_A0
#define MCP3202_DIN PIN_A1
#define MCP3202_CS PIN_A5 //* PIN_A2 is LED and TX_434
#define MCP3202_DOUT PIN_A3 //* PIN_A4 is RS232 TX
void adc_init() {
output_high(MCP3202_CS);
delay_us(5);
output_low(MCP3202_CS);
}
// basic ADC clock rate in this coding is 10 usec period => fclock = 100kHz
// fclock/18 ~ 5.5 kHz sample rate
void write_adc_byte(BYTE data_byte, BYTE number_of_bits) {
BYTE i;
for(i=0; i<number_of_bits; ++i) {
output_low(MCP3202_CLK);
if((data_byte & 1)==0)
output_low(MCP3202_DIN);
else
output_high(MCP3202_DIN);
data_byte=data_byte>>1;
delay_us(5);
output_high(MCP3202_CLK);
delay_us(5);
}
}
BYTE read_adc_byte(BYTE number_of_bits) {
BYTE i,data;
data=0;
for(i=0;i<number_of_bits;++i) {
output_low(MCP3202_CLK);
delay_us(5);
shift_left(&data,1,input(MCP3202_DOUT));
output_high(MCP3202_CLK);
delay_us(5);
}
return(data);
}
long int read_analog_mcp(BYTE channel, BYTE mode) {
int l;
long int h;
BYTE ctrl_bits;
// if (mode != 0) mode=1; //nonzero = single channel mode
if (channel != 0) channel=1;
output_low(MCP3202_CLK);
output_high(MCP3202_DIN);
output_low(MCP3202_CS);
ctrl_bits=channel; //set MSFB=0 (bit 1) & channel number (0 or 1)
ctrl_bits=ctrl_bits<<1;
if(mode) // single channel mode if nonzero
ctrl_bits |= 1;
else // In differential mode
ctrl_bits &= 0xfe;
ctrl_bits=ctrl_bits<<1; // Shift so LSB is start bit
ctrl_bits |= 1; // start bit
write_adc_byte( ctrl_bits, 5); // Send the control bits (start+3+null bit)
h=read_adc_byte(8);
l=read_adc_byte(4)<<4;
output_high(MCP3202_CS);
return((h<<8)|l)>>4; //create and right justify result
}
long int read_analog( BYTE channel ) // Auto specifies single mode
{
return read_analog_mcp( channel, 1);
}