Problems with analogRead / Grove Sound Sensor


#1

Hi,

I have some problems using a Grove Sound Sensor (or Grove Temperature Sensor) connected to VIN, GND and A0. The sonsors are running fine with my arduino, but with a particle (connected to usb) the analogRead function returns strange readings. In the following sections you can see my code and the result.

#define GROVE_PIN   A0

void setup() {
    Serial.begin(9600);

    long now = (long) millis();
    while( (!Serial.available()) && (( ((long) millis()) - now) < 5000) ) {
        Particle.process();
    }

    Serial.println("started...");

    pinMode(GROVE_PIN, INPUT);  
}

void groveSound(int val) {
    int sensorValue = val;

    Serial.print("val: ");
    Serial.println(sensorValue);
    if(sensorValue > 200) {
        Serial.println("on");
        delay(1000);
    }
}

void loop() {
    groveSound( analogRead(GROVE_PIN) );
    delay(10);
    //Serial.println("");

    Spark.process();

    //delay(1000);
}

Output (there is no sound at all, Arduino has values between 10 - 20):

started...
val:92
val:75
val:57
val:57
val:61
val:74
val:77
val:71
val:86
val:88
val:106
val:124
val:129
val:152
val:164
val:180
val:222
on
val:85
val:66
val:59
val:68
val:78
val:69
val:71
val:103
val:98
val:79
val:86
val:91
val:148
val:176
val:237
on
val:195
val:207
on
val:92
val:124
val:149
val:155
val:172
val:161
val:144
val:134
val:148
val:167
val:176
val:202
on
val:166
val:156
val:161
val:120
val:92
val:73
val:117
val:155
val:155
val:160
val:207
on
val:68
val:61
val:49
val:50
val:52
val:93
val:115
val:118
val:123
val:136
val:143
val:169
val:171
val:191
val:200
val:179
val:190
val:222
on
val:207
on
val:167
val:183
val:183
val:150
val:132
val:128
val:135
val:125
val:110
val:110
val:148
val:177
val:179
val:160
val:158
val:172
val:188
val:206
on
val:124
val:117
val:101
val:77
val:76
val:77
val:64
val:61
val:92
val:119
val:109
val:113
val:123
val:142
val:172
val:210
on
val:159
val:121
val:89
val:96
val:131
val:164
val:160
val:142
val:122
val:118
val:142
val:171
val:205
on
val:78
val:66
val:80
val:95
val:99
val:74
val:70
val:115
val:157
val:155
val:142
val:144
val:152
val:133
val:140
val:184
val:239
on
val:61
val:70
val:148
val:125
val:97
val:81
val:127
val:212
on
val:145
val:113
val:109
val:130
val:167
val:151
val:163
val:191
val:194
val:170
val:158
val:125
val:140
val:167
val:194
val:198
val:184
val:182
val:193
val:202
on
val:181
val:174
val:194
val:209
on

I tried several firmware versions, but that does not fix the problem.
Any ideas?

Greetings,
Sandy


#2

@Sandy, typically Grove sensors are powered by 5V which Arduinio works with. The Photon analog inputs CANNOT and MUST NOT be driven with a voltage higher than 3.3v, which may explain the strange results. You need a resistor voltage divider between the sensor output and the A0 pin.

Another factor to keep in mind is that Arduino uses 10bit (0-1023) ADCs while the Photon uses 12bit (0-4095) ones.


#3

@peekay123: Thanks for your quick reply. I do have a fixed direct connection between the grove data pin and A0 on my board. Is there any chance to fix this?


#4

@peekay123: Is there a way to use A0 as a digital input pin in a way that it is 5V tolerant?


#5

@Sandy, you need to disconnect it as it may damage the A0 input pin if it hasn’t done so already. The fix requires reducing the voltage from the Grove output so it doesn’t exceed 3.3v. The simplest way is to use a simple resistive voltage divider with two resistors. You could use a 10K and 18Kohm resistor as follows:

Grove output --- 10K ohm---- Photon A0
                         |
                        18K ohm
                         |
                        GND

Obviously there are other combinations of resistors which can provide the same results but these are common values. Using this approach, you will need to mathematically scale the value from the Grove to “correct” the values.


#6

I set it up exactly like you said with 5V supply to the sensor:

    sensor output -> 10k -> A0
    10k (the end going into A0) -> 20K -> GND

I also converted sensor output to 12 bit ( sensor_val * 5 / 4096 ).

But I am still getting random values. The sensor doesn’t seem to even respond to increase in sound!

Any thoughts?

Cheers