AVR Based Datalogger with MATLAB Interface

2017, Oct 09    

Introduction

As a junior at Oregon State University I enrolled in ECE 341 Junior Design. In this class I completed three projects. Each project lasted three weeks. The MATLAB sensor program was my first project and I was partnered with three other students to work on this project.

Requirements of the Project

  • For this project the team must write both a MATLAB script and their own Arduino program.
  • The MATLAB script will display readings from a thermistor and photocell.
  • Both of these datasets are to be displayed real time.
  • For the thermistor dataset a linear approximation and the Steinhart-Hart model will be used to calculate temperature

Design

The team created a block diagram that described the major functions of the system. The sensing block describes the microcontroller converting the analog value of the sensor to a digital value. The communication block describes the microcontroller sending the data to the computer over UART. The processing block describes the computer running a MATLAB script that takes the digital sensor value and converting it to Celsius for the thermistor and Lux for the photocell. The script also plots the data vs time. Each team member took responsibility for a different block. My task was to write the MATLAB program and AVR code.

block diagram

Figure: Block Diagram of the system

The circuit connected the sensors to the ADC of the microcontroller. R3 and R6 are needed because the sensors will have low resistance at one of the extremes of their range. The sensor and resistor pair create a voltage divider. As the sensor changes resistance, the voltage on the ADC pin will also change.

schematic

Figure: Electrical circuit schematic

The circuit was soldered together on a protoboard. The circuit was laid out so that it could be used as a shield for the Arduino. This was a compact solution which reduced the need for jumper wires.

final product

Figure: Completed circuit attached to an Arduino Uno

One challenge when receiving data was knowing which sensor was being measured. To solve this problem code was added so the microcontroller did not take any measurements until it received a start signal from MATLAB. This way the MATLAB program would be ready to receive data and know that the first number received was measured from the photocell and the next measurement form the thermistor.

The code running on the microcontroller allowed for samples to be taken at a specified interval. The microcontroller utilizes an 8-bit Timer/Counter which generates an interrupt every 1 ms. This allows for many different sample rates to be obtained counting the number of interrupts. The data collected is then sent over the UART interface.

The MATLAB program has two different custom GUIs. The first GUI allows the user to select the COM port that the microcontroller is connected to. If the microcontroller is connected after the program is launched the “Scan for COM” button can be pressed to rescan for COM ports. This GUI also allows the user to input how many seconds the microcontroller should record for and how many samples per second to take. If a data field has an illegal value, then the box will be highlighted in red. If any box is red, then the GUI will not allow the user to start taking measurements.

setup gui

Figure: Setup GUI

setup gui with error

Figure: Setup GUI with no COM port found

Once the user clicks the “Start Taking Measurements” button on the first GUI, MATLAB will close the setup GUI and opens the graphing GUI. This new GUI starts serial communication with the microcontroller and sends the information entered in the setup GUI. Then the graphing GUI will listen to the microcontroller and wait for data to be sent. Once data is received the program will apply a linear approximation or the Steinhart-Hart model to the temp data. The program will plot the data for each sensor and each dataset will be analyzed to find the min and max and reports the timestep at which the min or max occurred.

graphing gui

Figure: Graphing GUI

Final Thoughts

The project went well, and we received an A on our project. The future steps for this program would be to transmit numbers in binary instead of as ASCII characters. This would decrease the time spend communicating and higher sampling rates could then be achieved.

https://github.com/mdennis070/AVR_MATLAB_datalogger