Component Tester
A device that can identify and measure the values of capacitors and resistors
Devices & Components
1
16x2 LCD display with I²C interface
1
Arduino Nano
2
diode
6
Resistors
1
LED (generic)
3
Transistor NPN (BC547 or similar)
2
spst relay
2
Push Button
Hardware & Tools
1
Soldering kit
Software & Tools
Arduino IDE
Project description
Code
Code for device
cpp
May need to change some things
1#include <Wire.h> 2#include <LiquidCrystal_I2C.h> 3 4 5const int readPin = A6; 6const int VINpin = A7; 7const int switchPin = 4; 8const int zeroPin = 9; 9const int medRPin = 2; 10const int largeRPin = 3; 11const int goPin = 10; 12 13float rVal; 14float Vin; 15long resistorVal; 16 17 18 19long offsetTime = 0; 20unsigned long startTime; 21unsigned long elapsedTime; 22unsigned long displayTime = 0; 23 24 25LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display 26 27// remove any unnused series 28const int E24[] = { 29 10,11,12,13,15,16, 30 18,20,22,24,27,30, 31 33,36,39,43,47,51, 32 56,62,68,75,82,91 33}; 34 35const int E12[] = { 36 10,12,15,18, 37 22,27,33,39, 38 47,56,68,82 39}; 40 41const int E6[] = { 42 10,15,22,33,47,68 43}; 44// unique symbols 45uint8_t ohmSymbol[8] = { 46 0x00, 47 0x0E, 48 0x11, 49 0x11, 50 0x11, 51 0x0A, 52 0x1B, 53 0x00 54}; 55 56uint8_t MuSymbol[8] = { 57 0x00, 58 0x0A, 59 0x0A, 60 0x0A, 61 0x0E, 62 0x09, 63 0x08, 64 0x00 65}; 66void setResistor(int resistorMode) { 67 if (resistorMode == 1) { 68 resistorVal = 9830.0; 69 digitalWrite(largeRPin, LOW); 70 digitalWrite(medRPin, HIGH); 71 } 72 73 if (resistorMode == 2) { 74 resistorVal = 1000000.0; 75 digitalWrite(largeRPin, HIGH); 76 digitalWrite(medRPin, LOW); 77 } 78} 79void setup() { 80 // put your setup code here, to run once: 81 Serial.begin(9600); 82 Wire.begin(); 83 pinMode(readPin,INPUT); 84 pinMode(VINpin,INPUT); 85 pinMode(switchPin,OUTPUT); 86 pinMode(zeroPin,INPUT); 87 pinMode(medRPin,OUTPUT); 88 pinMode(largeRPin,OUTPUT); 89 pinMode(goPin,INPUT); 90 digitalWrite(zeroPin,HIGH); 91 digitalWrite(goPin,HIGH); 92 93 lcd.init(); 94 lcd.createChar(0, ohmSymbol); 95 lcd.createChar(1, MuSymbol); 96 97 98 99 100} 101bool timeOut = false; 102void loop() { 103 // put your main code here, to run repeatedly: 104 105 bool readResistance = false; 106 107 // turn off screen if time moer than 10000 millis 108 if((millis() - displayTime) > 10000 && timeOut == true){ 109 lcd.clear(); 110 lcd.home(); 111 lcd.print("turning off"); 112 delay(1000); 113 lcd.noBacklight(); 114 lcd.noDisplay(); 115 timeOut = false; 116 } 117 118 119 120 121 bool zeroVal = digitalRead( zeroPin); 122 // calibrate zero for more accuracy 123 if(zeroVal == 0) { 124 bool exit = 0; 125 Serial.print("zerore"); 126 setResistor(1); 127 delay(20); 128 Vin = analogRead(VINpin); 129 startTime = micros(); 130 while (exit == 0){ 131 rVal = analogRead(readPin); 132 if(rVal >= (Vin * 0.632)){ //leave loop if vout is more than 63.2% (one time constant) of input voltage 133 offsetTime = micros() - startTime; 134 exit = true; 135 } 136 } 137 lcd.backlight(); 138 lcd.display(); 139 lcd.clear(); 140 lcd.home(); 141 lcd.print("Zero Calibrated"); 142 timeOut = true; 143 displayTime = millis(); 144 } 145 bool goVal = digitalRead(goPin); 146 if(goVal == 0){ 147 lcd.display(); 148 lcd.backlight(); 149 lcd.clear(); 150 lcd.print("reading..."); 151 Serial.println("starting..."); 152 153 //check if its resistor 154 digitalWrite(switchPin,HIGH); 155 delay(100); 156 digitalWrite(switchPin,LOW); 157 delay(20); 158 setResistor(2); 159 delay(20); 160 rVal = analogRead(readPin); 161 delay(200); 162 int new_rVal = analogRead(readPin); 163 if(abs(new_rVal - rVal)<= 1){ 164 //double check its resistor and not just rlly big capacitor 165 digitalWrite(switchPin,HIGH); 166 delay(100); 167 digitalWrite(switchPin,LOW); 168 delay(20); 169 setResistor(1); 170 delay(20); 171 rVal = analogRead(readPin); 172 delay(200); 173 new_rVal = analogRead(readPin); 174 if(abs(new_rVal - rVal)<= 1) { 175 readResistance = true; 176 Serial.println("reading ressistnace"); 177 } 178 } 179 180 Serial.print("rVal : "); 181 Serial.println(rVal); 182 Serial.print("new Rval : "); 183 Serial.println(new_rVal); 184 //read capacitance if its not resistor 185 if(!readResistance){ 186 bool has_reset = false; 187 Vin = analogRead(VINpin); 188 int threshold = Vin * 0.632; // 63.2 % of max voltage (one time constant) 189 Serial.println(threshold); 190 191 192 setResistor(1); 193 RESET: // label to jump to if reset needed 194 bool exit = 0; 195 delay(10); 196 Serial.print("R = "); 197 Serial.println(resistorVal); 198 //discharge cap before 199 digitalWrite(switchPin,HIGH); 200 delay(200); 201 startTime = micros(); //start timer 202 digitalWrite(switchPin,LOW); 203 // loop until vout is 63.2% of input voltage 204 while (exit == 0){ 205 rVal = analogRead(readPin); 206 207 if(rVal >= (threshold)){ //leave loop if vout is more than 63.2% (one time constant) of input voltage 208 elapsedTime = micros() - startTime; 209 exit = true; 210 } 211 212 213 } 214 Serial.print("Exit rVal = "); 215 Serial.println(rVal); 216 217 elapsedTime -= offsetTime; 218 // read again with larger resistor if elapsed time < 1500 AND if loop hasnt already been reset 219 if(elapsedTime < 1500 && !has_reset){ 220 setResistor(2); 221 Serial.println("resest"); 222 has_reset = true; 223 delay(20); 224 goto RESET; 225 } 226 Serial.print("Time : "); 227 Serial.println(elapsedTime); 228 //calculate capaitance 229 float capacitance = ((float)elapsedTime / 1000000.0) / resistorVal; 230 capacitance *= 1000000000; // convert farad to nanofarad 231 Serial.println(capacitance); 232 float roundedCap = roundToSeries(capacitance,E6,sizeof(E6) / sizeof(E6[0])); // round to nearest value 233 234 //discharge capacitor 235 digitalWrite(largeRPin, LOW); 236 digitalWrite(medRPin, LOW); 237 digitalWrite(switchPin,HIGH); 238 delay(500); 239 digitalWrite(switchPin,LOW); 240 Serial.println(roundedCap); 241 // print rounded capacitance and true capacitance to lcd 242 lcd.clear(); 243 lcd.home(); 244 lcd.print("True: "); 245 printCapacitance(capacitance); 246 lcd.setCursor(0,1); 247 lcd.print("Rounded: "); 248 printCapacitance(roundedCap); 249 } 250 251 // if its a resistor 252 if (readResistance) { 253 digitalWrite(switchPin, LOW); // Make sure the transistor is OF 254 Serial.println("Reading Resistance"); 255 256 setResistor(1); 257 delay(20); // Let relay settle 258 259 Vin = analogRead(VINpin); 260 rVal = analogRead(readPin); 261 // Auto-range 262 if (rVal > 600) { 263 setResistor(2); 264 delay(20); 265 Vin = analogRead(VINpin); 266 rVal = analogRead(readPin); 267 } 268 long rvalSum = 0; 269 for (int i = 0; i < 128; i++) { 270 analogRead(readPin); 271 rvalSum += analogRead(readPin); 272 } 273 rVal = rvalSum / 128.0; 274 float rVoltage = rVal * (5.0/1023.0); 275 Serial.print("rvoltage : "); 276 Serial.println(rVoltage); 277 float resistance = (rVal * resistorVal) / (Vin - rVal); 278 Serial.print("Vin = "); 279 Serial.println(Vin); 280 281 Serial.print("rVal = "); 282 Serial.println(rVal); 283 Serial.print("Resistance = "); 284 Serial.println(resistance); 285 286 long roundedR = roundToSeries(resistance, E24, sizeof(E24) / sizeof(E24[0])); 287 lcd.clear(); 288 lcd.home(); 289 lcd.print("True: "); 290 printResistance(resistance); 291 lcd.setCursor(0, 1); 292 lcd.print("Rounded: "); 293 printResistance(roundedR); 294 digitalWrite(medRPin,LOW); 295 digitalWrite(largeRPin,LOW); 296 } 297 timeOut = true; 298 displayTime = millis(); 299 } 300 301 302 303} 304 305float roundToSeries(float value, const int series[], int size) { 306 307 long decade = 1; 308 309 while (value >= decade * 10L) 310 decade *= 10L; 311 312 float normalized = (value * 10.0) / decade; 313 314 int closest = series[0]; 315 316 for (int i = 1; i < size; i++) { 317 if (fabs(series[i] - normalized) < fabs(closest - normalized)) 318 closest = series[i]; 319 } 320 321 if (fabs(normalized - 100.0) < fabs(normalized - closest)) 322 return decade * 10.0; 323 324 return (closest * decade) / 10.0; 325} 326void printResistance(float value) { 327 328 if (value >= 1000000) { 329 value /= 1000000.0; 330 if (value == int(value)) 331 lcd.print(value,0); 332 else 333 lcd.print(value,1); 334 lcd.print("M"); 335 } 336 else if (value >= 1000) { 337 value /= 1000.0; 338 if (value == int(value)) 339 lcd.print(value,0); 340 else 341 lcd.print(value,1); 342 lcd.print("k"); 343 } 344 else { 345 lcd.print(value,0); 346 } 347 lcd.write(0); 348} 349void printCapacitance(float value) { 350 351 352 if (value >= 1000.0) { 353 value /= 1000.0; 354 if (value == int(value)) lcd.print(value,0); 355 else lcd.print(value,1); 356 lcd.write(1); 357 } else { 358 if(value == int(value)) lcd.print(value,0); 359 else lcd.print(value,1); 360 lcd.print("n"); 361 } 362 363 lcd.print("F"); 364}
Downloadable files
Enclosure Box
Custom enclosure
Tester_box.stl
Enclosure Top
Based on my specific hardware, may need modificati
tester Top.stl
Label
Label that fits into slot of box
Label.stl
Comments
Only logged in users can leave comments