Week 16:

Interface and Application Programming

Objectives:


Intro

The assignment of the week was to develope an application able to display or interact with an input/output connected to a board we created.
I decided to use the board i created for the week 11 ( Machine design ) which integrate an header to read a general NTC100k thermistor.

More info about the board HERE

Board Side

Internally the board have a voltage divider of 100k ohm and the thermistor input. The voltage divider output is connected to the ATmega 328p pin ADC1 which correspond to the pin A1 on the Arduino IDE.

I used the following code to convert the analog reading of the thermistor ( 0 to 1023 ) to a readable Celsius temperature using the Steinhart-Hart Thermistor Equation in pull-up configuration. After each reading i sent the measurement via serial to the PC using an FTDI to USB cable.


#include < math.h >
#define LED 2
#define POTENTIOMETER A1
int temp = 0;
void setup() {
  pinMode(LED, OUTPUT);
  pinMode(POTENTIOMETER, INPUT);
  Serial.begin(9600);
}

void loop() {
  temp = Thermistor(analogRead(POTENTIOMETER));
  Serial.println(temp);
  if(temp>33) digitalWrite(LED, HIGH); // If the temp is above 33 degree turn on the onboard led
  else digitalWrite(LED, LOW);     
}

double Thermistor(int RawADC) {
 double Temp;
 Temp = log(10000.0/(1024.0/RawADC-1)); 
 Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp );
 Temp = Temp - 273.15;            // Convert Kelvin to Celcius
 return Temp;
}
            

Application/Interface Side

On the computer side i decided to use Processing https://processing.org/ which is a programming language oriented to graphical interfaces.
The code read the serial stream, each line is a new thermistor reading, and output the data as a continuous bar plot.
If the temperature is below 33 degree the plot is solid blue, otherwise it become red.


import processing.serial.*;

Serial myPort;
int xPos = 1;
float inByte = 0;

void setup () {
  size(400, 300);
  println(Serial.list());
  myPort = new Serial(this, "/dev/tty.usbserial-FT90HLWP", 9600);
  myPort.bufferUntil('\n');
  background(0);
}
void draw () {
  // draw the line:
  if(inByte < 100)
  stroke(127, 34, 255);
  else 
  stroke(255, 34, 40);
  line(xPos, height, xPos, height - inByte);

  if (xPos >= width) {
    xPos = 0;
    background(0);
  } else {
    xPos++;
  }
}


void serialEvent (Serial myPort) {
  String inString = myPort.readStringUntil('\n');

  if (inString != null) {
    inString = trim(inString);
    inByte = float(inString);
    println(inByte);
    inByte = map(inByte, 0, 100, 0, height);
  }
}