Skip to content

Instantly share code, notes, and snippets.

@pingswept
Last active June 28, 2018 10:52
Show Gist options
  • Save pingswept/5201193 to your computer and use it in GitHub Desktop.
Save pingswept/5201193 to your computer and use it in GitHub Desktop.
Code for reading voltages with the Precision Voltage Shield, an Arduino shield for reading very precisely. 8 channels; 14-bit, 16-bit, and 18-bit versions. More details at http://rascalmicro.com/blog/2013/03/21/bringing-an-analog-voltage-arduino-shield-to-life/
// Copyright 2014, Brandon Stafford, brandon@rascalmicro.com
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. The name of the author may not be used to endorse or promote products
// derived from this software without specific prior written permission.
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <SPI.h>
#define RESOLUTION 16
#if RESOLUTION == 14
#define SCALE_FACTOR 0.0006103515625
#endif
#if RESOLUTION == 16
#define SCALE_FACTOR 0.000152587890625
#endif
#if RESOLUTION == 18
#define SCALE_FACTOR 0.00003814697265625
#endif
// 10/(2^14) = 0.0006103515625
// 10/(2^16) = 0.000152587890625
// 10/(2^18) = 0.00003814697265625
#define BUSY 3
#define RESET 4
#define START_CONVERSION 5
#define CHIP_SELECT 10
#define MISO 12
#define LED 13
#define TOTAL_RAW_BYTES RESOLUTION
int bytesToRead = TOTAL_RAW_BYTES;
byte raw[TOTAL_RAW_BYTES];
signed long parsed[8];
void setup() {
pinMode(BUSY, INPUT);
pinMode(RESET, OUTPUT);
pinMode(LED, OUTPUT);
pinMode(START_CONVERSION, OUTPUT);
pinMode(MISO, INPUT);
Serial.begin(115200);
SPI.begin();
digitalWrite(START_CONVERSION, HIGH);
digitalWrite(CHIP_SELECT, HIGH);
digitalWrite(RESET, HIGH);
delay(1);
digitalWrite(RESET, LOW);
}
void loop() {
int i;
digitalWrite(START_CONVERSION, LOW);
delayMicroseconds(10);
digitalWrite(START_CONVERSION, HIGH);
while (digitalRead(BUSY) == HIGH) {
// wait for conversion to complete
}
digitalWrite(CHIP_SELECT, LOW);
while (bytesToRead > 0) {
raw[TOTAL_RAW_BYTES - bytesToRead] = SPI.transfer(0x00);
bytesToRead--;
}
digitalWrite(CHIP_SELECT, HIGH);
bytesToRead = TOTAL_RAW_BYTES;
parseRawBytes();
//Serial.write(raw, 16);
for(i=0; i<8; i++) {
Serial.print((float)parsed[i] * SCALE_FACTOR, 5);
Serial.print(",");
}
Serial.print("\r\n");
delay(1000);
}
void parseRawBytes() {
int i;
#if RESOLUTION == 14
parsed[0] = (raw[0] << 6) + (raw[1] >> 2) & 0x3FFF;
parsed[1] = (raw[1] << 12) + (raw[2] << 4) + (raw[3] >> 4) & 0x3FFF;
parsed[2] = (raw[3] << 10) + (raw[4] << 2) + (raw[5] >> 6) & 0x3FFF;
parsed[3] = (raw[5] << 8) + (raw[6] << 0) & 0x3FFF;
parsed[4] = (raw[7] << 6) + (raw[8] >> 2 ) & 0x3FFF;
parsed[5] = (raw[8] << 12) + (raw[9] << 4) + (raw[10] >> 4) & 0x3FFF;
parsed[6] = (raw[10] << 10) + (raw[11] << 2) + (raw[12] >> 6) & 0x3FFF;
parsed[7] = (raw[12] << 8) + (raw[13] << 0) & 0x3FFF;
#endif
#if RESOLUTION == 16 // 16-bit code not tested yet
parsed[0] = (raw[0] << 8) + (raw[1] >> 0);
parsed[1] = (raw[2] << 8) + (raw[3] >> 0);
parsed[2] = (raw[4] << 8) + (raw[5] >> 0);
parsed[3] = (raw[6] << 8) + (raw[7] >> 0);
parsed[4] = (raw[8] << 8) + (raw[9] >> 0);
parsed[5] = (raw[10] << 8) + (raw[11] >> 0);
parsed[6] = (raw[12] << 8) + (raw[13] >> 0);
parsed[7] = (raw[14] << 8) + (raw[15] >> 0);
#endif
#if RESOLUTION == 18
parsed[0] = (raw[0] << 10) + (raw[1] << 2) + (raw[2] >> 6) & 0x3FFFF;
parsed[1] = (raw[2] << 12) + (raw[3] << 4) + (raw[4] >> 4) & 0x3FFFF;
parsed[2] = (raw[4] << 14) + (raw[5] << 6) + (raw[6] >> 2) & 0x3FFFF;
parsed[3] = (raw[6] << 16) + (raw[7] << 8) + (raw[8] >> 0) & 0x3FFFF;
parsed[4] = (raw[9] << 10) + (raw[10] << 2) + (raw[11] >> 6) & 0x3FFFF;
parsed[5] = (raw[11] << 12) + (raw[12] << 4) + (raw[13] >> 4) & 0x3FFFF;
parsed[6] = (raw[13] << 14) + (raw[14] << 6) + (raw[15] >> 2) & 0x3FFFF;
parsed[7] = (raw[15] << 16) + (raw[16] << 8) + (raw[17] >> 0) & 0x3FFFF;
#endif
for(i=0; i<8; i++) {
parsed[i] = fixSignBit(parsed[i]);
}
}
long fixSignBit(long reading) {
#if RESOLUTION == 14
if(reading & 0x2000) { // if reading is < 0 (stored as two's complement)
return reading | 0xFFFFC000; // set bits 31-14
} else {
return reading;
}
#endif
#if RESOLUTION == 16 // 16-bit code not tested yet
if(reading & 0x8000) { // if reading is < 0 (stored as two's complement)
return reading | 0xFFFF0000; // set bits 31-16
} else {
return reading;
}
#endif
#if RESOLUTION == 18
if(reading & 0x20000) { // if reading is < 0 (stored as two's complement)
return reading | 0xFFFC0000; // set bits 31-18
} else {
return reading;
}
#endif
}
@amrithmmh
Copy link

can you share me the shematic and pcb board for precision voltage shield ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment