Tuesday, December 26, 2017

Harbor Freight / Pittsburgh Caliper Arduino Connection

Another HF Caliper project. Here is the HF Caliper connected to our digital interface for Arduino, Raspberry Pi, and more. Just above the battery door is a small access panel that covers a 4 pin edge connector. Carefully solder 3 wires to those pads, which, from left to right are GND, Data, Clock, and +1.5v (we aren't using that one). You no longer need to solder on to those delicate pads, as we have a cable that plugs right in!

The interface is simple. two transistors (we used 2N2222A) and four 10k Ohm resistors 1/4w.

You can order a finished board for only $20, a HF cable (no soldering) for $25, and a Arduino Nano w/ integrated Screw Shield for $15

Screenshot, schematic, and code are posted below. Thanks to the following sites for ideas and resources:

http://www.instructables.com/id/Reading-Digital-Callipers-with-an-Arduino-USB/

http://www.makingstuff.info/Projects/Digital_Calipers

Cable to fit caliper, eliminates need to solder - https://littlemachineshop.com/products/product_view.php?ProductID=3483&category=132430212

Just cut off the RJ-11 plug, strip 1" of the black outer jacket, strip 1/2" of insulation on each wire, fold back in half, solder, and insert into screw terminals (the wires are too fine to hold in the screw terminals otherwise). Connect as follows:

Gnd - yellow
Data - blue
Clock - red
1.5v (not used) - white

Next version of the board will have a RJ-11 jack on board, no stripping wires.








Arduino code is below:

//Digital caliper code to read the value off of a cheap set of digital calipers
//By Making Stuff Youtube channel https://www.youtube.com/c/makingstuff
//This code is open source and in the public domain.

const byte clockPin = 2;  //attach to clock pin on calipers
const byte dataPin = 3; //attach to data pin on calipers

//Milliseconds to wait until starting a new value
//This can be a different value depending on which flavor caliper you are using.
const int cycleTime = 32; 

unsigned volatile int clockFlag = 0; 

long now = 0;
long lastInterrupt = 0;
long value = 0;

float finalValue = 0;
float previousValue = 0;

int newValue = 0;
int sign = 1;
int currentBit = 1;

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

  pinMode(clockPin, INPUT);  
  pinMode(dataPin, INPUT); 
  
  
  //We have to take the value on the RISING edge instead of FALLING
  //because it is possible that the first bit will be missed and this
  //causes the value to be off by .01mm.
  attachInterrupt(digitalPinToInterrupt(clockPin), clockISR, RISING);
}

void loop() {  
  
  if(newValue) 
  {
   if(finalValue != previousValue) {
     previousValue = finalValue;
     Serial.println(finalValue,2);     
   }
   newValue = 0;
  }
  
 //The ISR Can't handle the arduino command millis()
 //because it uses interrupts to count. The ISR will 
 //set the clockFlag and the clockFlag will trigger 
 //a call the decode routine outside of an ISR.
 if(clockFlag == 1)
 {
  clockFlag = 0;
  decode();
 } 
  
}

void decode(){
   unsigned char dataIn;
   dataIn = digitalRead(dataPin); 
   
   now = millis();
   
   if((now - lastInterrupt) > cycleTime)
   {
     finalValue = (value * sign) / 100.00;
     currentBit = 0;
     value = 0;
     sign = 1;
     newValue = 1;      
   } 
   else if (currentBit < 16 )
   {
      
     if (dataIn == 0)
     {
       if (currentBit < 16) {
          value |= 1 << currentBit;
       }  
       else if (currentBit == 20) {
          sign = -1;
       }
               
     }
     
     currentBit++;
     
   }
   
   lastInterrupt = now;
   
}

void clockISR(){
 clockFlag = 1; 
}







No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...