//LCD config
#include<LiquidCrystal.h>
LiquidCrystal lcd(A5, A4, A3, A2, A1, A0);
//13 is the input to the circuit (connects to 150ohm resistor), 11 is the comparator/op-amp output.
unsigned long pulse, frequency, capacitance, inductance;
void setup() {
lcd.begin(16, 2);
// lcd.backlight();
Serial.begin(9600);//115200
pinMode(10, INPUT);
pinMode(13, OUTPUT);
lcd.print(" Arduino ");
delay(2000);
lcd.clear();
Serial.println("Why hello!");
delay(200);
}
void loop() {
digitalWrite(13, HIGH);
delay(5);//give some time to charge inductor.
digitalWrite(13, LOW);
delayMicroseconds(100); //make sure resination is measured
pulse = pulseIn(10, HIGH,5000); //returns 0 if timeout
Serial.print(pulse); #The pulse what I am getting here is always zero.
if (pulse > 0.1)
{ //if a timeout did not occur and it took a reading:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Pulse=");
lcd.print(pulse);
delay(1000);
//error insert your used capacitance value here. Currently using 2uF. Delete this line after that
capacitance = 2.E-6; // - insert value here
frequency = 1.E6 / (2 * pulse);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Frequency=");
lcd.print(frequency);
delay(1000);
inductance = 1. / (capacitance * frequency * frequency * 4.*3.14159 * 3.14159); //one of my profs told me just do squares like this
inductance *= 1E6; //note that this is the same as saying inductance = inductance*1E6
//Serial print
Serial.print("High for uS:");
Serial.print( pulse );
Serial.print("\tfrequency Hz:");
Serial.print( frequency );
Serial.print("\tinductance uH:");
Serial.println( inductance );
delay(10);
//LCD print
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Inductance:");
lcd.setCursor(0, 1);
lcd.print(inductance);
lcd.setCursor(14, 1);
lcd.print("uH");
delay(10);
pulseIn() will wait until the state changes from LOW to HIGH and then measure time until the state changes from HIGH to LOW. I suspect that your input is already HIGH when you start looking for the start of the pulse. If it doesn't see both changes before the timeout (5 milliseconds) it will return 0.
Do you have an oscilloscope you can use to see if the signal on the input pin is doing what the article says it should do?
This is the output which I am getting after simulating the project in proteus.
As you can see the blue colored waveform is the signal that I am sending it to arduino for giving me the pulsewidth of positive half cycle. The time taken to complete half a cycle is 50us(blue waveform) which should have been the output receiving from the pulseIn function. But instead I am getting zero.
Replying to your suggestion- What can I do so that arduino reads LOW first?Should I try giving a delay?
The scope only shows the 1.5 milliseconds after the the signal goes HIGH. You aren't looking at the input then. You wait 5 milliseconds, then set the output LOW, and then wait another 100 microseconds before looking at the input. What does that part of the waveform look like?
This is the complete waveform. The yellow signal (with on-time=5ms and off-time=100us) is given to LC circuit which is then fed to opamp that converts the resonating red sine wave to a blue square wave with 50% duty cycle (with each half cycle =50us). Now this blue square is fed to arduino input pin 10 where pulseIn is defined. Even though it is within the limit of 10us-3min timeout, I am not able to get the output.
Also In the above snapshot the yellow waveform is not showing result as required i.e we wanted a signal of 5ms high and 100us/0.1ms low but may be because of the pulsein function the off time is more(4ms).
Is that the case or anything else?
It looks like the time period you are looking at (100 microseconds after the falling edge) has a good signal in the simulation. My guess is that the physical circuit is not acting the same as the simulated circuit. Do you have an oscilloscope you can use to check the physical circuit?
What do you mean by good signal? A signal with 100us period can be detected by pulseIn function right?
Also I am giving the blue signal as the input to arduino.