I describe in this project how to build a tiny data logger by connecting different types of switches directly to Hexabitz uSD module (H1BR60) and logging all of them to a single microSD card log. The project teaches you how to run multiple logs with different configurations and multiple variables at the same time. Four sensors/switches are connected to a single H1BR60 (microSD card) module: Two mechanical limit switches, one optical endstop switch and one magnetic switch.
What is it?Four sensors/switches are connected to a single H1BR60 module: Two mechanical limit switches, one optical endstop switch (used in 3d printers) and one magnetic reed switch (mechanical as well, usually used for doors etc.) This example particularly shows how the PortButtons BOS feature can be used for many different implementations other than just reading user buttons. We will be running two logs of different types simultaneously, one is rate-based and the other is event-based.
Estimated hardware build time: 5 minutes
Estimated software development time: 5-10 minutes
The port switch/button functionality is a BOS (Bitz Operating System, the underlying Hexabitz backend) feature that converts array serial ports into I/O ports that can sample external switches via an easy-to-use API. In this example, we define the four switches and associate each one with a different event.
AddPortButton(MOMENTARY_NO, P1); // Magnetic
SetButtonEvents(P1, 0, 0, 1, 0, 0, 1, 0, 0);
AddPortButton(MOMENTARY_NO, P3); // Limit Switch 1
SetButtonEvents(P3, 1, 0, 3, 0, 0, 0, 0, 0);
AddPortButton(MOMENTARY_NO, P4); // Limit Switch 2
SetButtonEvents(P4, 1, 0, 3, 0, 0, 0, 0, 0);
AddPortButton(MOMENTARY_NC, P5); // Optical
SetButtonEvents(P5, 1, 0, 1, 0, 0, 1, 0, 0);
The optical switch is bit different than the other ones. It is not a pure mechanical switch that connects and disconnects signals. The way BOS port button feature works is by outputting a 1 (3.3V) on port TXD pin and reading it back on RXD pin. If a mechanical switch caused these two lines to connect together, it will be detected. The optical switch has 3 pins: power, ground and signal output. Nevertheless, I hacked it into a port button/switch by connecting its power pin to port TXD (the switch is 3.3V-5V compatible), its signal pin to port RXD and its ground to module GND. So basically it'll be powered for a short amount of time but it's enough to generate a signal and measure it. This works well because the endstop a simple switch with no time delays or complications.
Then we create below an event-based log and log all the switches:
CreateLog("Event", EVENT, 10, FMT_COMMA, FMT_SAMPLE, "#");
LogVar("Event", PORT_BUTTON, P1, "Magnetic");
LogVar("Event", PORT_BUTTON, P3, "LS1");
LogVar("Event", PORT_BUTTON, P4, "LS2");
LogVar("Event", PORT_BUTTON, P5, "Optical");
Logging is then started and stopped after 30 seconds. The indicator LED is used to show some blinks!
IND_ON();
StartLog("Event");
Delay_s(30);
StopLog("Event");
IND_OFF(); // Signal the end of the log
Delay_ms(300); IND_blink(200);
Delay_ms(300); IND_blink(200);
The event-based log can be easily replaced with a rate-based log as follows:
CreateLog("Rate", RATE, 10, FMT_SPACE, FMT_SAMPLE, "#");
You can also run both logs at the same time and start / stop them simultaneously or independently. Using the internal RTC and calendar available in all Hexabitz modules, you can setup a time-based log with time stamps as well. Check below for some screenshots of time-based logs. The demo code and example logs are attached to this project.
Comments