// Program for attiny3216 to Hall magnetic sensor and display it on 4x20 LCD liquid crystal // created by Jans Hendry // Universitas Gadjah Mada, Indonesia // Kamakura Node, Japan // fab academy MIT - 2022 // the directives/predecessor #include #include // create lcd as an object // format: address, num of columns, num of rows LiquidCrystal_I2C lcd(0x27, 20, 4); // sensor pin const int tp_pin = 4; void setup() { // start serial Serial.begin(9600); // setting up lcd.init(); // turn on backlight <- the variable resistor has been adjusted lcd.backlight(); // clear screen lcd.clear(); // static display defDisplay(); // delay delay(500); } void loop() { int sdata = analogRead(tp_pin); // read analogue data int sensdata = 1023 - sdata; // Serial.print("Sensor reading: "); // Serial.println(sensdata); lcd.setCursor(12, 0); lcd.print(sdata); // raw value // calculate magnetic field in B float vout = (sensdata / 1024.0) * 5; // voltage out float B = 1000 * ((2*vout/3) - 5/3); lcd.setCursor(12, 1); lcd.print((int)B); // calculate magnetic field in mB float MB = B / 1000; // let say mega B lcd.setCursor(12, 2); lcd.print((float)MB); delay(1000); // take a break every 1 second } void defDisplay() { lcd.clear(); // clear display // static text lcd.setCursor(0, 0); lcd.print("Read-out "); lcd.setCursor(11, 0); lcd.print(":"); lcd.setCursor(0, 1); lcd.print("Magnetic B"); lcd.setCursor(11, 1); lcd.print(":"); lcd.setCursor(19, 1); lcd.print("G"); lcd.setCursor(0, 2); lcd.print("Magnetic MB"); lcd.setCursor(11, 2); lcd.print(":"); lcd.setCursor(18, 2); lcd.print("MG"); }