I’ve finally built my Due and TFT into a nice housing and coded a reasonably well-working oscilloscope and datalogger for it. Here’s what I’ve learned along the way!
The oscilloscope showing analog input and digital output from an Arduino Uno powering a servo motor with the “knob” example sketch from the Arduino servo library.
Oscilloscope wiring and housing
As I wrote in my earlier post on the TFT screen, it took some time to get the screen connected and running. Afterwards, I felt it would be a good idea to build a custom housing and solder all the wires manually to “save some space and money”. So I basically spent 10 hours and a lot of wires to avoid buying a 10€ shield from SainSmart. And while I now have some basic knowledge on the connections on the TFT, this will be of little use in the future – what a waste of time!
This is the most important tip in this whole blogpost: if you want to reproduce this project, make sure you don’t make the same mistake – just buy a shield to connect microcontroller and TFT.
Anyway, my design now involves many wires fit into a small custom-built wooden box:
The Due with all the wires. Did I mention yet that a shield would have been helpful?
The Due’s analog pins on right side are accessible from the housing’s wall (female wire pins on the left side). Since the Due only allows 3.3 V, two pins are divided between their input and GND with resistors – A0 with a 1:1 ratio, A1 with a 10:1 ratio.
The housing is made from balsa wood: light and easy to build, but not very robust. Also, the power and usb ports from the Due and the TFT’s SD slot are accessible from the outside.
The upper side of the box with 3.3V, 5V, GND, the 5 analog pins and two digital pins (currently not used, but you never know) accessible.
Programming the oscilloscope and datalogger
The sketch I’ve written brings several previous topics together:
- I’m using Henning Karlsen’s UTFT and URTouch libraries to power the display and and touchscreen.
- For datalogging functionality, I use the standard SD library (but might switch to SDFat in the future).
The sketch heavily uses global variables (not that great, I know…) and follows this structure:
Setup: Initialize TFT and Touch Screen, plot first the selection menu and then axes for the plot
Loop: 4-part structure that
a) first waits for a good starting point (unless continuous mode is enabled) by looking for a high-enough gain on the enabled analog channels
b) reads many analog values into the Due’s memory (and if continuos plotting is enabled, also plots them right after reading)
c) plots all values from the memory (unless already done at b)
d) checks for touch input: if available, either rescale the axes (if touch is close to the screen border) or show the menu (touch close to the middle of the screen)
Additional functions include:
A plotting function plotDataPoints that plots a single reading (accessed both while reading analog data for continuous plotting or in part c for triggered plotting (allows faster data aquisition). If logging is enabled, this function also passes the data to the file (even if plotting is disabled).
A function showMenu that pauses the Oscilloscope mode, plots the menu and waits for user input (select / deselect analog channels and other options)
A function startLogging that checks if an SD card is available and opens a file.
Putting it all together was comparably easy, thanks to the good documentation for the UTFT and URTouch libraries and the work I’ve put into other datalogger projects. It’s the first project I’ve been doing with such a large scope and I believe I’ve found a good solution that can easily modified and extended in the future. Also, the touchscreen works great, with fast and easy access to changes for the plot scale and the menu!
On the not-so-great side, I dislike the way my code is entangled – for example, the plotting function is called from two places in the loop function (depending on the settings chosen, see above). The decision to handle logging as part of the plotting function made things a lot messier – maybe there is a cleaner way to handle this?
See how it works!
I’m currently working on building an Arduino-powered RC vehicle (more on this in a future post). Since I’m partially using spare parts, the Oscilloscope is really helpful here to figure out what is going on:
Live plots of the control signal for the servo motor
I use a small servo motor for steering. To get started, I used the sweep example from the servo library which reads an analog voltage from a potentiometer and moves the servo to the corresponding angle position. The live plot is super-helpful to see what’s going on – see the video here to get an idea!
Logging voltage and current for the driver motor
As a driver, I use a motor from my stack of spare electric parts. It came without any datasheet so I basically figured out how to use it by trial-and-error (it turns out it works quite well for my purpose if I power it from a 9 V battery). So how large is the voltage drop when I power it – and how much current and power runs through the motor? That’s something you might measure directly with a multimeter but I can also use the continuos mode with logging to record voltage and current (the latter by measuring voltage gain on a 1 Ohm resistor). I’ve also added a light sensor to the wheel to capture the movement – it reacts to the changing brightness:
For logging, you can either use the continuos mode (resulting in a very unregular logging interval – maybe logging to a binary format can fix this?) or increase the amount of values in the triggered mode and use this for logging.
Summary and Sketch download
The oscilloscope works reasonably well and is a nice use for the Due and TFT, especially with the data logging function added and the high data rates possible (e.g. 100kHz for two analog signals when using the triggered mode). Considering the significant cost and work required (~40 € and many hours), I would recommend using an Uno / Nano and a connected Laptop or PC for visualization and logging of the data for most projects.
If you have a Due or Teensy board + TFT around, you can grab my Arduino sketch here.