Using USB memory stick with OPTA

From the OPTA docs "Additionally and using a USB memory stick, the USB-C® connector can be used for data logging
purposes or to update the program inside the PLC."

I've tried extensively with the USBHost library and cannot get the OPTA to read or even power a memory stick (OPTA powered externally).

The USBHost function 'supplyPowerOnVBUS' does not seem to have any effect.

Does anyone have an examples of this working?

2 Likes

Hi @tmbkr

A new tutorial on USB data logging with Arduino Opta will be released soon on Arduino Docs.

Stay tuned!

Best,

1 Like

Hello!

Such an OPTA PLC will arrive soon, to which it would be important to connect a USB data carrier. Where can I get the required library?

take a look here: Introducing Arduino's new storage libraries: Streamline data management for your projects | Arduino Blog

Example for OPTA: https://github.com/arduino-libraries/Arduino_USBHostMbed5/blob/main/examples/OptaDirList/OptaDirList.ino

For anyone who's interessted.
I used this code on an Arduino Opta RS485

/*
    SimpleStorageWriteRead

    Demonstrates basic usage of the "Arduino_UnifiedStorage" library to write and read data to storage.
    Supports SD card, USB storage, and internal storage (default, uncomment to choose).

    In the setup function, the code initializes serial communication, mounts the storage medium,
    creates a root directory with three subdirectories, and writes data to three files in each subdirectory.

    Following this, the code showcases reading data from files by using "seek" and "available" methods,
    switching file modes to read, resetting file pointers to the start,
    and printing the read data to the serial monitor using a while loop.

    INSTRUCTIONS
      1. Check compatibility with your board and make sure you have "POSIXStorage" and "Arduino_UnifiedStorage" installed
      2. Connect your board to the serial monitor
      3. Wait for the sketch to run 


    Created 28th July 2023
    By Cristian Dragomir

    Modified 24th August 2023
    By Ali Jahangiri

    https://github.com/arduino-libraries/Arduino_UnifiedStorage/blob/main/examples/SimpleStorageWriteRead/SimpleStorageWriteRead.ino

*/

#define ARDUINO_UNIFIED_STORAGE_DEBUG

#include "Arduino_UnifiedStorage.h"


void printFolderContents(Folder dir, int indentation = 0) {
  std::vector<Folder> directories = dir.getFolders();
  std::vector<UFile> files = dir.getFiles();

  // Print directories
  for (Folder subdir : directories) {
    for (int i = 0; i < indentation; i++) {
      Arduino_UnifiedStorage::testPrint("  ");
    }
    Arduino_UnifiedStorage::testPrint("[D] ");
    Arduino_UnifiedStorage::testPrint(subdir.getPath());
    printFolderContents(subdir, indentation + 1);
  }

  // Print files
  for (UFile file : files) {
    for (int i = 0; i < indentation; i++) {
      Arduino_UnifiedStorage::testPrint("  ");
    }
    Arduino_UnifiedStorage::testPrint("[F] ");
    Arduino_UnifiedStorage::testPrint(file.getPath());
  }
}


// Uncomment one of the three lines below to select between SD card, USB or internal storage
//SDStorage storage;             // Create an instance for interacting with SD card storage
USBStorage storage;            // Create an instance for interacting with USB storage
//InternalStorage storage;


void setup() {
  beginRS485(115200);

  // toggle this to enable debugging output
  Arduino_UnifiedStorage::debuggingModeEnabled = true;


  // storage = InternalStorage();
  // storage = SDStorage(); // Uncomment this line to use SD card storage
  storage = USBStorage(); // Uncomment this line to use USB storage

  if(!storage.begin()){
    Arduino_UnifiedStorage::testPrint("Error mounting storage device.");
  }
  
  // Create a root directory in storage device
  Folder root = storage.getRootFolder();

  // Create subdirectories inside the root directory
  Folder subdir1 = root.createSubfolder("subdir1");
  Folder subdir2 = root.createSubfolder("subdir2");
  Folder subdir3 = root.createSubfolder("subdir3");

  // Create .txt files inside the subdirectories
  UFile file1 = subdir1.createFile("file1.txt", FileMode::WRITE);
  UFile file2 = subdir2.createFile("file2.txt", FileMode::WRITE);
  UFile file3 = subdir3.createFile("file3.txt", FileMode::WRITE);

  // Write data to the files
  file1.write("This is file 1.");
  file2.write("This is file 2.");
  file3.write("This is file 3.");

  // Read data from the files using seek and available
  Arduino_UnifiedStorage::testPrint("Reading data from files using seek and available:");

  // Close and open files in reading mode
  file1.changeMode(FileMode::READ);
  file2.changeMode(FileMode::READ);
  file3.changeMode(FileMode::READ);


  // Read data from file1
  file1.seek(0); // Move the file pointer to the beginning
  while (file1.available()) {
  char data = file1.read();
    Arduino_UnifiedStorage::testPrint(String(data));
  }
  Arduino_UnifiedStorage::testPrint("");

  // Read data from file2
  file2.seek(0); // Move the file pointer to the beginning
  while (file2.available()) {
    char data = file2.read();
    Arduino_UnifiedStorage::testPrint(String(data));
  }
  Arduino_UnifiedStorage::testPrint("");

  // Read data from file3
  file3.seek(0); // Move the file pointer to the beginning
  while (file3.available()) {
    char data = file3.read();
    Arduino_UnifiedStorage::testPrint(String(data));
  }
  Arduino_UnifiedStorage::testPrint("");

  printFolderContents(storage.getRootFolder());
}

void loop() {
  //Flash LED to see that test finished
  digitalWrite(LEDG, !digitalRead(LEDG));
  delay(500);
}

USB Drive is attached via a USB A to USB C Adapater
i used RS-485 Interface to send debug messages.

Anyone else who already tried this?

Hello!
You can now access the new tutorial: "Using a USB Memory Stick for Data Logging on Opta™". Here is the tutorial link: https://docs.arduino.cc/tutorials/opta/usb-data-logging.

1 Like

Hi, I have an OPTA Lite and just tried this sketch. I uploaded with the Arduino IDE and now I can't reprogram the Opta anymore through the USB port. How do I recover the OPTA?

Hello @gjt211

Double click the reset button of your Opta. The red led will start to blink

That means that Opta is now in bootloader mode, connect it to your arduino ide and upload whatever sketch you are interested into, that should work"