CE 351 2020 Fall
Lab 4: LCD, Sensors, and Barebones Atmega328P
Nic Theobald
nstheobald@fortlewis.edu


LCD, Sensors, and Barebones Atmega328P

Introduction

This lab covers the use of basic displays and sensor. Digital, analog, and resistive temperature sensors were used along with a digital humidity sensor. In the end, a bare atmega328P was used to display temperature to an SSD, using interupts and a shift register.

Methods and Materials



Item
Quantity


Arduino IDE
Arduino UNO
Trim Potentiometer
LCD 1602

1
1
1
1
1


This lab started with a simple display of some arbitrary text to the LCD. It then moved on to moving the text around. Then some basic digital, analog, and resistive temperature sensors were introduced. In the end, the MCU was removed from the arduino and was used to determine the temperature using the DHT11 and a simple SSD. However, displaying to the SSD was not simple. A shift register was used to pass the single wire signal to all of the segments. Interupts were also used to momentarily sample temperature and siplay it to the SSD.

Results

Task 1 and 2: "Hello World" - LCD

A natural introduction to sensors and interacting with microcontrollers is displaying some arbitrary text to a display. Here, the string "hello world" was displayed on a LCD.
Some varibles were also displayed on the LCD. The arduino counts to five seconds and then resets.


Task 3: Move Text on LCD

To display enough text on the screen, it may be necissary to scroll the text around.



Task 4: Thermistor Temperature Sensor

My favorite method of sensing temperature was using the thermistor resistor. I wrote some code that, like normal, calculated the temperature, but also found the max, min, and average temperature. I put it in my fridge and found that one of the two were very innacurate. I think it was both but mostly the thermistor...







Task 5: Temp and Humidity Via DHT11

A digital temperature and humidity sensor may also be necissary.




Task 6: Temperature Via TMP36 Analog Sensor

An simple analog temperature sensor may be all that is needed.



Task 7: Controlling Arduino with IR Remote

An IR reciever was connected to the arduino and the LCD was instructed to display the meaning of any recieved signal.



Task 8: Flashing an LED with a Barebones Atmega328P

The atmega MCU was removed from the arduino and was placed in the bread board
. A simple "Flash LED" script was written and sent to the MCU using the TX and RX pins on the Arduino UNO.


Task 9: Building a Portable Temperature using Barebones Atmega, DHT11, and SSD.

The Barebones MCU was taken a step further with a temperature sensor, shift register, and SSD.
An interupt was used to momentarily sample temperature and send it to the shift regist
// Setup
int latchPin=11;// RCLK
int clockPin=9;// SRCLK
int dataPin=12;//SER

int D1=5;
int D2=4;
int D3=3;
int D4=2;

int ssddelay = 2;
int dig1 = B0;
int dig2 = B0;
int dig3 = B0;
int dig4 = B01110001;

int tempPin = A0;

byte SSDs=0x3F; // in binary, it is 0011 1111, which only turns off G and H, will display 0

void setup()
{
  Serial.begin(9600);
// Interrupts Stuff
  noInterrupts();
  TCCR1A=0; //Reset Timer/Counter1 Control Register A
  TCCR1B=0; //Reset Timer/Counter1 Control Register B
  TCNT1=0; // Reset Timer/Counter1
  OCR1A=6000; // Output Compare Register
  TCCR1B|=(1<<WGM12);
  TCCR1B|=(1<<CS12);
  TCCR1B|=(1<<CS10);
  TIMSK1|=(1<<OCIE1A);
  interrupts();

  pinMode(latchPin,OUTPUT);
  pinMode(dataPin,OUTPUT);
  pinMode(clockPin,OUTPUT);
 
// Setting The Digit Activation Pins
  pinMode(D1,OUTPUT);
  pinMode(D2,OUTPUT);
  pinMode(D3,OUTPUT);
  pinMode(D4,OUTPUT);
}

void loop()
{
// Assigns the binary output, SSDs, as a changing variable, dig1...
  SSDs = dig1;
  updateShiftRegister();
  digitalWrite(D1,LOW);
  delay(ssddelay);
  digitalWrite(D1,HIGH);
  delay(ssddelay);

// Assigns the binary output, SSDs, as a changing variable, dig2...
  SSDs = dig2;
  updateShiftRegister();
  digitalWrite(D2,LOW);
  delay(ssddelay);
  digitalWrite(D2,HIGH);
  delay(ssddelay);

 
  SSDs = dig3;  
  updateShiftRegister();
  digitalWrite(D3,LOW);
  delay(ssddelay);
  digitalWrite(D3,HIGH);
  delay(ssddelay);

// Assigns the binary output, SSDs, so that only the point is shown...
  SSDs = B10000000;  
  updateShiftRegister();
  digitalWrite(D2,LOW);
  delay(ssddelay);
  digitalWrite(D2,HIGH);
  delay(ssddelay);

// Assigns the binary output, SSDs, as "F"...
  SSDs = dig4;
  updateShiftRegister();
  digitalWrite(D4,LOW);
  delay(ssddelay);
  digitalWrite(D4,HIGH);
  delay(ssddelay);
}

// Function that handles the shift register...
void updateShiftRegister()
{
  digitalWrite(latchPin,LOW); // Stops the code code from being passed...
  shiftOut(dataPin,clockPin,MSBFIRST,SSDs); // Sends data while stepping clock...
  digitalWrite(latchPin,HIGH); // Sends all of the data to outputs...
}

// Simple function that assigns the previously mentioned "changing variable"...
void lookup(int num, int n){
  if(num == 0){
   num = B00111111;
  }
  else if(num == 1){
   num = B00000110;
  }
  else if(num == 2){
    num = B01011011;
  }
  else if(num == 3){
   num = B01001111;
  }
  else if(num == 4){
   num = B01100110;
  }
  else if(num == 5){
    num = B01101101;
  }
  else if(num == 6){
   num = B01111101;
  }
  else if(num == 7){
  num = B00000111;
  }
  else if(num == 8){
  num = B01111111;
  }
  else if(num == 9){
  num = B01111111;
  }

//  Serial.println(num);
//  Serial.println(n);

// Determines which variable to assign the binary too.
  if(n == 1){
    dig1 = num;
    Serial.print("dig1: ");
    Serial.println(dig1);
  }
  else if(n == 2){
    dig2 = num;
    Serial.print("dig2: ");
    Serial.println(dig2);
  }
  else if(n == 3){
    dig3 = num;
    Serial.print("dig3: ");
    Serial.println(dig3);
  }
}

// The actual interupt...
ISR(TIMER1_COMPA_vect){

// Temp Collections and Calculations
int sensorVal=analogRead(tempPin);
float voltage = (sensorVal/1024.0)*5.0;
float tempF = ((voltage-0.5)*100*9/5)+32;
//  Serial.println(tempF);

// Multiplies the temp by 10 to shift all relevant decimals.
// Then converts to integer...
  int tempint = (int)(tempF*10);
//  Serial.println(tempint);

  int num1 = tempint/100;
// Assignes first digit
  int num2 = tempint/10 - num1*10; // Assignes second digit
  int num3 = tempint - num1*100-num2*10; // Assignes third digit

// Runs the assignment function...
  lookup(num1, 1);
  lookup(num2, 2);
  lookup(num3, 3);   
}




Discussion:

This lab experimented with different sensors and how to display them. In the end, a more finalized approach was used to determine and display the temperature without the help of the actual arduino. The final task was quite difficult. Mostly because many of my mistakes were from mistyping a single letter or variable.