Reading Digital caliper with Arduino
#21
Chuck,

I don't get a compiler error when I insert the line of code Arvid suggested. Here's my complete program. I don't have the board yet, it should be here by Wednesday.

Ed

Code:
//Simple Digital Calliper Reader
//See http://j44industries.blogspot.com/

// Pin Declarations
int dataIn = 16;
int clockIn = 15;

// Variables
int clock = 1;
int lastClock = 1;
unsigned long time = 0;
unsigned long timeStart = 0;
int out = 0;


void setup() {
 // Pin Set Up
 pinMode(dataIn, INPUT_PULLUP);    
 pinMode(clockIn, INPUT_PULLUP);  


 Serial.begin(115200);
 Serial.println("Ready: ");
}


void loop(){

 lastClock = clock;
 clock = digitalRead(clockIn);

 if (lastClock == 1 && clock == 0){
   out = digitalRead(dataIn)+digitalRead(dataIn)+digitalRead(dataIn); // Tripple sampling to remove glitches
   Serial.println(micros());
   if((micros() - time) > 800){
     Serial.println(" ");
   }
   else if((micros() - time) > 400){
     Serial.print("  ");
   }

   if (out > 1){
     Serial.print("1");
   }
   else{
     Serial.print("0");
   }
   Serial.print(",");
   time = micros();
 }
}
Reply
Thanks given by:
#22
Oops! Arvid beat me to it.

Those level shifters will need pull up resistors on the collector of the transistors. Likely the microcontroller has them built in but probably need to be programmed to activate them. I'm not familiar with Arduino but have been meaning to dive into it so I'll take a look at seeing how to activate the pull up resistors on those two inputs.

Oh, and please keep this thread going. If people aren't interested in it they don't have to read it.

Ed
Reply
Thanks given by:
#23
Chuck,

Change your setup function as follows. The INPUT_PULLUP mode enables the pull-up resistors on the clock and data lines. It compiled fine but of course I couldn't test it on hardware yet.

Ed

Code:
void setup() {
  // Pin Set Up
  pinMode(dataIn, INPUT_PULLUP);    
  pinMode(clockIn, INPUT_PULLUP);
  
  Serial.begin(115200);
  Serial.println("Ready: ");
}
Reply
Thanks given by:
#24
Chuck,

If the drawing you posted for the level shifter is correct and you copied the code from Instructables then you need to change this code:

// Pin Declarations

int dataIn = 11;
int clockIn = 12;

To this:

// Pin Declarations

int dataIn = 16;
int clockIn = 15;

You're using different pins for the inputs than the Instructables example is using thus the need for the code change.
Another thing to consider is that the level shifter you posted will invert the signals coming from the digital caliper so the code will have to change to compensate for that.

Ed
Reply
Thanks given by:
#25
I missed a few posts, as I took advantage of the warmer weather and worked in the shop a while today.
Going back to post 21, Ed I copied your code and it worked after I changed the data and clock  pins to match my hookup. That is I get an output of numbers on the serial monitor, but I don't know what I'm seeing. The caliper currently reads -2.57mm. Attachment is a screen capture of the serial monitor. I unplugged the Arduino to make the screen capture. I think the output s just a count of milliseconds, right?


Chuck


Attached Files Thumbnail(s)
   
Micromark 7x14 Lathe, X2 Mill , old Green 4x6 bandsaw
The difficult takes me a while, the impossible takes a little longer.
Reply
Thanks given by:
#26
Chuck,

Did you add the level shifter circuitry you posted?

Ed
Reply
Thanks given by:
#27
yes, level shifters included.

Chuck
Micromark 7x14 Lathe, X2 Mill , old Green 4x6 bandsaw
The difficult takes me a while, the impossible takes a little longer.
Reply
Thanks given by:
#28
Good, we now have output.

What the application is doing is "every time it determines that the clock line has changed from 1 to 0 it prints the value of the micro timer". The micro timer is simply a counter telling us the number of microseconds that have elapsed since the program started. We can safely ignore the timer overflow that occurs every 70 minutes of so.

What the output says is that at  62008, 2109720, 4157352, 6205176 and 8252628 microseconds, or about every 2 seconds, the clock changed from a 1 to a 0 and the value of the data line when this happened was 1.

Based on the information we have from the instructable and Yuri's site we were expecting transitions in the "several thousand transitions per second" range, not the "once every 2 seconds" we are seeing.

The only conclusion I can draw from this is that the clock input on the Arduino is still not getting the correct clock information from the caliper.

You might try changing the sense of the clock transitions ... from 0 to 1 rather than from the current 1 to 0 and see what we get. Change the line:

if (lastClock == 1 && clock == 0) {

to 

if (lastClock == 0 && clock == 1) {

and rerun the application. But I am not expecting any great insight to come from it.
Reply
Thanks given by:
#29
I changed the code per the above, but now have a different problem. The scale has stopped reading. It's displaying 0.00 with no change when moved. Got to T/S this new problem, maybe change calipers.

Chuck
Micromark 7x14 Lathe, X2 Mill , old Green 4x6 bandsaw
The difficult takes me a while, the impossible takes a little longer.
Reply
Thanks given by:
#30
Fixed the caliper. Removed the voltage divider power input and reinstalled the battery. Caliper is now reading. Loaded new code and no output on serial other than Ready:

Chuck
Micromark 7x14 Lathe, X2 Mill , old Green 4x6 bandsaw
The difficult takes me a while, the impossible takes a little longer.
Reply
Thanks given by:




Users browsing this thread: 1 Guest(s)