Category Archives: Arduino

Solved: Initial Arduino Opta setup woes

I had great difficulty getting my Arduino Opta set up and working using the Arduino PLC IDE – whatever I tried I got the “Cannot download Sketch file (error code: 1)” error.

I finally had success using the plain Arduino IDE to do the initialization and then switching to the Arduino PLC IDE. The final step was changing the Modbus address to 247.

It probably shouldn’t be that tricky….

Updated: Arduino Opta

I ordered an Arduino Opta (RS485 version) and it has arrived – stay tuned for upcoming posts!

In the meantime, I’ve updated my initial review as it has been confirmed that the digital inputs support 24VDC. Programming support for IEC 61131-3 PLC languages has also been clarified (no MacOS support sadly).

ProsConsUnknowns
Partnership with FinderDigital I/O uses 0-10VDC, not “standard” 24VDCHow will programming support for IEC 61131-3 PLC languages work? Via existing IDE?
DIN rail mountAnalogue inputs use 0-10VDC, not “standard” 4-20mACan programming be done over Ethernet?
IEC 61131-3 PLC language supportI/O expansion options?
(What does the “AUX” port do?)
Digital inputs support 24VDC (see here)
Beefy relay outputs with dedicated commons
Modbus-TCP and Modbus-RTU support
Input power supply is 12-24VDC
IP20
-20 °C to +50 °C temperature operating range

Initial Thoughts: Arduino Opta

Arduino have announced a new micro PLC, the Opta, and while some details are unclear, it seems like a pretty neat product. Here are my initial thoughts:

ProsConsUnknowns
Partnership with FinderDigital I/O uses 0-10VDC, not “standard” 24VDCHow will programming support for IEC 61131-3 PLC languages work? Via existing IDE?
DIN rail mountAnalogue inputs use 0-10VDC, not “standard” 4-20mACan programming be done over Ethernet?
IEC 61131-3 PLC language supportI/O expansion options?
Beefy relay outputs with dedicated commons
Modbus-TCP and Modbus-RTU support
Input power supply is 12-24VDC
IP20
-20 °C to +50 °C temperature operating range

I’m hoping to buy one.

TempMonHTTP

For a while I’ve been tinkering around with a simple project – an Arduino-based temperature (and humidity) monitor that outputs a webpage on my home LAN. The Arduino I used was the Freetronics EtherTen, a quality product.

Screenshot of the TempMonHTTP webpage. Yes, it gets hot in my walk in robe where the home LAN switch is…

Check out the code at https://github.com/gjhmac/TempMonHTTP.

Arduinos and SD cards

If you require an SD card interface for your Arduino project I highly recommend purchasing an Arduino with one built-in.  It took three attempts to get an SD card interface added to my basic Arduino Mega:

  1. A very cheap generic (“LC Studio”) break out module that seemed to work intermittently at best;
  2. A cheap shield (linksprite SD Card Shield v1.0b) – not compatible with the Mega (it does not state this on the packaging but if you search the documentation on their website it clearly states this, I should have done my research first);
  3. And finally, a not so cheap Ethernet plus SD card shield (Freetronics) that worked without a hitch.

I would have been better off spending the extra coin on a Mega with the SD card interface built-in and reclaiming most of my weekend…

[Part 7] Arduino Data Logger

// ————————————————————————————————————————————————
// Project:   DataLogger
// Version:   0.4
// Date:      26 August 2018
// Author:    Greg Howell <gjhmac@gmail.com>
// ————————————————————————————————————————————————
// Version    Date              Comments
// 0.4        26 August 2018    Modified code to only log to the SD card if the new value is different to the old value
// 0.3        30 June 2018      Added debugging and diagnostics on serial port, sped up ADC for analogue read (128kHz -> 1MHz), fixed “A REF”
// 0.2        26 April 2018     Addition of switch to enable/disable logging to SD card and LED to indicate logging status
// 0.1        17 February 2018  Initial Development
//
// ————————————————————————————————————————————————
// Description:
//  – Logs analog0 value to a text file on the SD card along with date/time stamp in CSV format
//  – Maintains date/time via DS1302 Real Time Clock
//  – Builds with Arduino 1.8.5
// ————————————————————————————————————————————————

// #includes
#include <SPI.h>    // Serial Peripheral Interface
#include <SD.h>     // SD Cards
#include <DS1302.h> // DS1302 RTC

const int chipSelect = 4;
const int buttonPin = 5;  // Pin 5 is the button to enable/disable logging (digital input)
const int ledPin =  6;    // Pin 6 is the LED indicate logging status (digital output)

const byte PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
const byte PS_16 = (1 << ADPS2);

int buttonState = 0;      // initialise button state to off
int oldsensor;            // variable to store the previous sensor value (used in loop())

// Init the DS1302
// Pin 2 = RST
// Pin 3 = DAT
// Pin 4 = CLK
DS1302 rtc(2, 3, 4);
// ————————————————————————————————————————————————
// setup()
// ————————————————————————————————————————————————
void setup() {

  ADCSRA &= ~PS_128;  // remove prescale of 128
  ADCSRA |= PS_16;    // add prescale of 16 (1MHz)

  analogReference(EXTERNAL);  // Analogue reference set to “A REF” pin

  pinMode(buttonPin, INPUT);  // Initialize the pushbutton pin as an input
  pinMode(ledPin, OUTPUT);    // Initialize the LED pin as an output

  rtc.halt(false);            // Set the clock to run-mode
  rtc.writeProtect(false);    // and disable the write protection

  Serial.begin(9600);

  // Use following lines once to set clock if battery fails (modify to suit)
  //rtc.setDOW(SUNDAY); // Set Day-of-Week to FRIDAY
  //rtc.setTime(21, 50, 0); // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(26, 8, 2018); // Set the date to August 6th, 2010

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // Print current system date from RTC at start up
  Serial.print(“System date: “);
  Serial.println(String(rtc.getDateStr()) + “,” + String(rtc.getTimeStr()));

  Serial.print(“Initializing SD card…”);

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println(“Card failed, or not present”);
    // don’t do anything more:
    while (1);
  }
  Serial.println(“card initialized.”);
}
// ————————————————————————————————————————————————
// loop()
// ————————————————————————————————————————————————
void loop() {
  String dataString = “”;                 // make a string for assembling the data to log
  int sensor = analogRead(A0);            // read analogue
  dataString += String(sensor);           // construct string with analogue signal
  buttonState = digitalRead(buttonPin);   // read button state

  // Logging enabled
  if (buttonState == HIGH) {
    File dataFile = SD.open(“datalog.txt”, FILE_WRITE);

    // if the file is available, write to it
    if (dataFile) {

      // if the new data is different to the old data write it to file
      if (sensor != oldsensor) {
        // Write data to serial output
        Serial.println(String(rtc.getDateStr()) + “,” + String(rtc.getTimeStr()) + “,” + dataString);
        Serial.println(String(sensor) + “,” + String(oldsensor));
        // Write data to SD card
        dataFile.println(String(rtc.getDateStr()) + “,” + String(rtc.getTimeStr()) + “,” + dataString);
        dataFile.close();
      }
      else {
        dataFile.close();
      }
    // set logging LED to high
    digitalWrite(ledPin, HIGH);
  }
  // if the file isn’t open, print an error
  else {
    digitalWrite(ledPin, LOW);
    Serial.println(“error opening datalog.txt”);
  }
}
  // Logging disabled
  else {
    // set logging LED to low
    digitalWrite(ledPin, LOW);
  }
  // set the old sensor value to the current sensor value (read at top of loop())
  oldsensor = sensor;
  // Wait before repeating 🙂
  delay (500);
}

[Part 6] Arduino Data Logger

// ————————————————————————————————————————————————
// Project:   DataLogger
// Version:   0.3
// Date:      30 June 2018
// Author:    Greg Howell
// ————————————————————————————————————————————————
// Version    Date              Comments
// 0.3        30 June 2018      Added debugging and diagnostics on serial port, sped up ADC for analogue read (128kHz -> 1MHz), fixed “A REF”
// 0.2        26 April 2018     Addition of switch to enable/disable logging to SD card and LED to indicate logging status
// 0.1        17 February 2018  Initial Development
//
// ————————————————————————————————————————————————
// Description:
//  – Logs analog0 value to a text file on the SD card along with date/time stamp in CSV format
//  – Maintains date/time via DS1302 Real Time Clock
//  – Builds with Arduino 1.8.5
// ————————————————————————————————————————————————

// #includes
#include    // Serial Peripheral Interface
#include     // SD Cards
#include // DS1302 RTC

const int chipSelect = 4;
const int buttonPin = 5;  // Pin 5 is the button to enable/disable logging (digital input)
const int ledPin =  6;    // Pin 6 is the LED indicate logging status (digital output)

const byte PS_128 = (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
const byte PS_16 = (1 << ADPS2);

int buttonState = 0;      // initialise button state to off

// Init the DS1302
// Pin 2 = RST
// Pin 3 = DAT
// Pin 4 = CLK
DS1302 rtc(2, 3, 4);
// ————————————————————————————————————————————————
// setup()
// ————————————————————————————————————————————————
void setup() {
  
  ADCSRA &= ~PS_128;  // remove prescale of 128
  ADCSRA |= PS_16;    // add prescale of 16 (1MHz)

  analogReference(EXTERNAL);  // Analogue reference set to “A REF” pin
  
  pinMode(buttonPin, INPUT);  // Initialize the pushbutton pin as an input
  pinMode(ledPin, OUTPUT);    // Initialize the LED pin as an output

  rtc.halt(false);            // Set the clock to run-mode
  rtc.writeProtect(false);    // and disable the write protection

  Serial.begin(9600);

  // Use following lines once to set clock if battery fails (modify to suit)
  //rtc.setDOW(THURSDAY); // Set Day-of-Week to FRIDAY
  //rtc.setTime(15, 50, 0); // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(26, 4, 2018); // Set the date to August 6th, 2010

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // Print current system date from RTC at start up
  Serial.print(“System date: “);
  Serial.println(String(rtc.getDateStr()) + “,” + String(rtc.getTimeStr()));

  Serial.print(“Initializing SD card…”);

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println(“Card failed, or not present”);
    // don’t do anything more:
    while (1);
  }
  Serial.println(“card initialized.”);
}
// ————————————————————————————————————————————————
// loop()
// ————————————————————————————————————————————————
void loop() {
  String dataString = “”;                 // make a string for assembling the data to log
  int sensor = analogRead(A0);            // read analogue
  dataString += String(sensor);           // construct string with analogue signal
  buttonState = digitalRead(buttonPin);   // read button state

  // Logging enabled
  if (buttonState == HIGH) {
    File dataFile = SD.open(“datalog.txt”, FILE_WRITE);

    // if the file is available, write to it:
    if (dataFile) {
      // Write data to serial output
      Serial.println(String(rtc.getDateStr()) + “,” + String(rtc.getTimeStr()) + “,” + dataString);
      // Write data to SD card
      dataFile.println(String(rtc.getDateStr()) + “,” + String(rtc.getTimeStr()) + “,” + dataString);
      dataFile.close();
      digitalWrite(ledPin, HIGH);
    }
    // if the file isn’t open, pop up an error:
    else {
      digitalWrite(ledPin, LOW);
      Serial.println(“error opening datalog.txt”);
    }
  }
  // Logging disabled
  else {
    digitalWrite(ledPin, LOW);
  }

  // Wait before repeating 🙂
  delay (200);
}