arduino read potentiometer variable resistor

 

capture

Salam dan hi,

Kali ni aku nak buat arduino yang baca resistor dan tunjukkan sebagai percentage. Experiment ni akan guna arduino uno R3 dan LCD Keypad Shield daripada DFROBOT. Bagi aku guna LCD Shield ni lagi best daripada sambung jumper wire ke breadboard. Tak serabut kepala.

350px-arduino_shield8
https://www.dfrobot.com/wiki/index.php/Arduino_LCD_KeyPad_Shield_(SKU:_DFR0009)

 

Untuk guna shield ni, just tukar code kepada – > LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

Coding aku ambil dari sini :  http://www.instructables.com/id/Arduino-analog-input-display/?ALLSTEPS .

 

Cuma aku ubah

// divide by 1.02 to get percentage
    potValue2 = potValue1 / 1.02;

Kepada

// divide by 1.02 to get percentage
    potValue2 = potValue1 / 1.023;

sebab berikut:

“The microcontroller of the board has a circuit inside called an analog-to-digital converter or ADC that reads this changing voltage and converts it to a number between 0 and 1023. ” – https://www.arduino.cc/en/Tutorial/ReadAnalogVoltage

 

So coding, u all boleh dapat dari instructable tu, atau copy paste seperti dibawah:

#include <LiquidCrystal.h> // include the LCD library
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

int potPin = A1; //Potentiometer input pin
int potValue1 = 0;
int potValue2 = 0; // final display variable
void setup() {
    lcd.begin(16, 2); // lcd rows and columns
    lcd.print(“Potentiometer”); // title of sorts
}

void loop() {
// read then divide the input(max 1020 in this case) by 10
    potValue1 = analogRead(potPin) / 10;
// divide by 1.02 to get percentage
    potValue2 = potValue1 / 1.023;
// set cursor to second row, first column
    lcd.setCursor(0, 1);
//display final percentage
    lcd.print(potValue2);
//print the percent symbol at the end
    lcd.print(“%”);
//wait 0.1 seconds
    delay(100);
//wipe the extra characters
    lcd.print(” “);
    delay(1);

 

Credit for coding goes to Mr. TheTeslaWarrior .

 

 

Leave a comment

Your email address will not be published. Required fields are marked *