Brandon Roots
  • INTERACTIVE
  • FILM
  • ABOUT
  • CONTACT
October 18, 2020

Lab: Intro to Asynchronous Serial Communications

broots ITP, Physical Computing

In this lab I took a closer look at asynchronous serial communication between an Arduino and a computer. This type of digital communication makes use of serial transmit (TX) and receive (RX) over IO ports to communicate at a set data rate.

The lab demonstrated that there are a number of ways for an Arduino to transmit data, including ASCII (alpha numeric characters) and binary code. Also the lab demonstrated that there are many methods to access the serial port on a computer outside of the Arduino serial monitor and serial plotter functions.

Using the Terminal application on my mac I made a serial connection to my Nano 33 IoT and was able to display the IMU (Inertial Measurement Unit) data that I set it to print over serial.

I also tried out “less /dev/cu.usbmodem14101” in terminal, which was meant to stop the window from auto scrolling, but received an error that “/dev/cu.usbmodem14101 is not a regular file (use -f to see it)”.

Getting to work in the terminal this week was great and I enjoyed expanding my skills there! I was also doing work in the terminal this week creating a Twitter Bot running on a Raspberry Pi for Programming A to Z with Daniel Shiffman.

For the next steps I made use of the IMU on the Nano 33 IoT and a push button, wired LOW to ground when in a neutral state and HIGH when pressed 😉. I put together the following Arduino sketch to create a well formatted output of sensor read values to the serial monitor. This sketch builds on the sample sketch “SimpleAccelerometer” by Riccardo Rizzo.

#include <Arduino_LSM6DS3.h>

int buttonValue = 0;
int buttonPin = 2;

void setup() {
  Serial.begin(9600);
  while (!Serial);

  // initialize pin 2 as pushbutton input
  pinMode(buttonPin, INPUT);

  if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");

    while (1);
  }

  Serial.print("Accelerometer sample rate = ");
  Serial.print(IMU.accelerationSampleRate());
  Serial.println(" Hz");
  Serial.println();
  Serial.println("Acceleration in G's");
  Serial.println("X\tY\tButton");
}

void loop() {
  float x, y, z;

  buttonValue = digitalRead(buttonPin);

  if (IMU.accelerationAvailable()) {
    IMU.readAcceleration(x, y, z);
    
    Serial.print(x);
    Serial.print(",\t");
    Serial.print(y);
    Serial.print(",\t");
    Serial.println(buttonValue);
    delay(100);
  }
}

Modifying the code a bit I implemented a handshake into the serial output from the Arduino so that it would only send values when the Serial became available:

#include <Arduino_LSM6DS3.h>

int buttonValue = 0;
int buttonPin = 2;

void setup() {
  Serial.begin(9600);
  while (Serial.available() <= 0) {
     Serial.println("hello"); // send a starting message
     delay(300);              // wait 1/3 second
   }
  while (!Serial);

  // initialize pin 2 as pushbutton input
  pinMode(buttonPin, INPUT);

  if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");

    while (1);
  }

  Serial.print("Accelerometer sample rate = ");
  Serial.print(IMU.accelerationSampleRate());
  Serial.println(" Hz");
  Serial.println();
  Serial.println("Acceleration in G's");
  Serial.println("X\tY\tButton");
}

void loop() {
  if (Serial.available()) {
    // read the incoming byte:
    int inByte = Serial.read();
      
    float x, y, z;
  
    buttonValue = digitalRead(buttonPin);
  
    if (IMU.accelerationAvailable()) {
      IMU.readAcceleration(x, y, z);
      
      Serial.print(x);
      Serial.print(",\t");
      Serial.print(y);
      Serial.print(",\t");
      Serial.println(buttonValue);
      delay(100);
    }
  }
}

Oddly the result here was for the serial readout to the computer to receive several values at once. I imagine this is because the serial port takes some time to turn on and off.

This lab provided quite a bit of information that will take some time to digest. I can certainly see how helpful it will be to consider the format of data over serial especially in the context of developing applications to read that data and making the whole process as efficient as possible.

On to the next lab!

I Had A Dream Bot Lab: Serial Input to P5.js

Related Posts

Fractal Plant – Foiled by  Registers

Homemade Hardware, ITP, Solar Plant

Fractal Plant – Foiled by Registers

Since receiving the PCBs and successfully soldering the board together I have been trying to rewrite code for the I2C port expander. This has been immensely difficult! The Inkplate Arduino Library makes considerable use of an “Mcp” class, which is written to work with the MCP23017 GPIO expander IC. These chips are quite difficult to […]

“Handling” Playtest Week

Handling, ITP

“Handling” Playtest Week

Last week we attended “Playtest Thursday” on the second floor of 370 Jay St with our games. I came away from the experience with some very specific feedback. Seeing a number of people play the game showed me things I didn’t anticipate. Some folks approached the cabinet and immediately treated it as a touch screen. […]

Fractal Plant – Beta Build

Homemade Hardware, ITP, Solar Plant

Fractal Plant – Beta Build

The boards arrived! Amazingly within an hour of one another. Based on the experience I think that JLCPCB is a better value. With shipping OSHPark was $55.50 for 3 boards. JLCPCB was $26.36 for 10 boards. Aside from a higher cost OSHPark also left sharp bits of tabs around the edges of the boards which […]

Recent Posts

  • Fractal Plant – Foiled by  RegistersFractal Plant – Foiled by Registers
    May 9, 2022
  • “Handling” Playtest Week“Handling” Playtest Week
    May 5, 2022
  • Fractal Plant – Beta BuildFractal Plant – Beta Build
    April 24, 2022
Brandon Roots