In an Arduino sketch, analogRead(A0) is the same as analogRead(14).
You can call grovepi.analogRead(0) or grovepi.analogRead(14)and you will get the same result.
GrovePi sockets A0,A1,A2 use the AD converter and supportanalogRead() values 0-1023.
GrovePi sockets D2-D8 are digital and support 1-bit input/output, values 0-1, using digitalRead() and digitalWrite().
GrovePi sockets D3,D5,D6 also support Pulse Width Modulation (PWM) which means you can write 8-bit values 0-255 with analogWrite().
Sadly you can’t use analogRead() with D3,D5,D6 and can only use it with A0,A1,A2 (aka D14,D15,D16).
grovepi.analogRead() uses the above aliases so if you are trying to read a value from an analog sensor connected to D3, analogRead(3) will actually read from the 2nd pin on the A2socket.
I understand everything except for the last line. Why would analogRead(3) read from A2? I thought it would add 14 to it, but still, A3 or A17 neither is connected to anything useful as I understand it?
I’m a little closer with getting the terms now, but I think I still don’t really understand why the library would interpret a command for analogRead of a port that isn’t on the GrovePi+ (A3) as a read from a different pin on the A2 socket. Is this the only way to reach line 26 on the ATmega328? Is that for a sensor with two ADCs connected to it?
And to follow along the pattern now, What does analogRead(4) and further instance (5, 6, 7) do?
Sorry, I want to make it clear: I don’t need this info in the sense that I’m trying to build something using that port or command right now, so take your time. I’m just trying to understand the architecture for hobbyist reasons.
Hi Mike,
I’ll try my best explaining the how the ports work on the GrovePi. We use an Atmega328P on the GrovePi and the firmware is compiled using an Arduino IDE. The python code simply sends commands to the GrovePi, the GrovePi interprets the commands and runs it on an Arduino code. The Arduino code allows the firmware to be compiled assuming that the chip has all the ports accessible in the same way as an Arduino Uno. Even though we have more pins left out on the Atmega328 on the GrovePi, we were not able to use them because we didn’t have enough real estate to put that many Grove connectors. For example, on the atmega 328p (the chip on the GrovePi) there are actually 7 ADC lines, but we are only using 4 of them A0 pin ,A1 pin ,A2 pin and A3 pin connected like 0,1 on A0 grove port, 1,2 on A1 grove port and 2,3 on A2 grove port (Sorry for the confusion around the pins and port. Pins come from the chip and port is the Grove Port. Each port has 2 pins). We are using 2 ADC lines for I2C because they are multiplexd and 2 ADC lines are unused.
If you do something like analogRead(4) or analogRead(5), that might break things sice we use those 2 lines for I2C comms between the Pi and the grovePi, but something like analogRead(6) and analogRead(7) would return you values from the pins which are not connected to anything. Even if you give it bigger numbers, it’ll give you garbage values and won’t crash.
We could have gone through the effort of only letting people use the ports which are accessible on the GrovePi but we left the ports open so that people using the GrovePi in custom applications still have access to all the ports if they want to.
Hope that this was not to confusing. Fell free to ask any questions and do let us know what you think about this.