Week 6
Assignment
- Use a sensor from the JVC kit or the available loanable components to measure a physical quantity using Arduino.
- Calibrate the sensor and show the conversion relationship between the measured and indicated value in a graph or table.
- Document the circuit schematic and include the source code on your website. (Use
code snippetsfor source code formatting in IDF.)
Sensor
I decided to explore Hall Effect sensor since I have zero experience with it.
I also used Display, basic Button
and connected it all to Arduino Nano.
Initial testing
I got inspired by students in previous years to connect the display correctly. I also found this PDF,
which also had an example code provided.
Almost all worked well, but I did not manage to get the potentiometer to work on the Hall sensor. I don't know if I did something wrong, but the analog value did not change in the slightest when rotating the potentiometer.
Schema
Pull up resistor
The pull up resistor is not necessary since Arduino Nano has it already implemented internally for digital pins.
But beware! The pin mode must be set to INPUT_PULLUP. pinMode(BUTTON, INPUT_PULLUP)
Code
#include "U8glib.h"
#define HALL_SENSOR A0
#define HALL_SENSOR_DIGITAL 7
#define BUTTON 2
#define UNCERTAINTY 20
U8GLIB_SSD1306_128X64 display(U8G_I2C_OPT_NONE);
long int overpaint = 0;
bool buttonPressed = false;
bool calibrated = false;
int thresholdValue = -1;
void setup(void) {
Serial.begin(9600);
pinMode(BUTTON, INPUT_PULLUP);
}
void loop(void) {
if (millis()-overpaint > 100) {
display.firstPage();
do {
if (!calibrated) {
drawNotCalibrated();
} else {
int hall_value = analogRead(HALL_SENSOR);
draw(hall_value);
}
} while( display.nextPage() );
overpaint = millis();
}
// button up
if (buttonPressed && digitalRead(BUTTON) == HIGH) {
if (calibrated) {
calibrated = false;
} else {
calibrated = true;
thresholdValue = analogRead(HALL_SENSOR);
}
buttonPressed = false;
}
if (digitalRead(BUTTON) == LOW) {
buttonPressed = true;
}
delay(10);
}
void drawNotCalibrated() {
display.setFont(u8g_font_unifont);
display.setPrintPos(0, 10);
display.print("Put a magnet on");
display.setPrintPos(0, 25);
display.print("the side of the");
display.setPrintPos(0, 40);
display.print("box and press");
display.setPrintPos(0, 55);
display.print("the button.");
}
void draw(int value) {
display.setFont(u8g_font_unifont);
display.setPrintPos(0, 10);
if (value > thresholdValue - UNCERTAINTY && value < thresholdValue + UNCERTAINTY) {
display.print("A magnet is");
display.setPrintPos(0, 25);
display.print("placed on the");
display.setPrintPos(0, 40);
display.print("board.");
} else {
display.print("Place a magnet on");
display.setPrintPos(0, 25);
display.print("the board!!!!");
}
}
Result