Categories
Uncategorized

Arduino 10MR & 10MT PLC modules

I’ve been seeing these Arduino-based “PLCs” on the internet for a while. If you’re not familiar with PLCs, they are basically control computers with built-in or expandable Inputs and Outputs and are used for industrial control. They are typically hardened against accidental overload (too much current on the output or wrong polarity voltage on inputs) and run on 24VDC They’re built to work reliably in a harsh industrial environment. They are also usually quite expensive, although there are low-priced versions available, such as the Click PLC starting around $100 each.

There have been industrialized versions of Arduino-based controllers for years. I have built a few projects based on the Industruino, for example, and I quite like it.

But these new versions like the 10MT and the 10MR address the lower end of the market that doesn’t require that level of “toughness”. I was curious, so I picked up a couple. They’re cheap, right, so how can you go wrong?

10MR PLC

First impressions are that it looks pretty good. It’s mountable either by DIN rail or four screw holes in the feet. The material seems to be ABS plastic. Polycarbonate would have been nice. Programming is via a DE-9 serial port, so you will need to connect it to a serial port. A USB-to-serial converter should work just fine. The connections are by rising-cage screw terminals, which was a nice surprise. Most Arduino clone connectors use the super cheap and annoying terminals that can’t grip wire properly unless you use ferrules. I mean, these are cheap terminals, but not the bottom of the barrel. These actually work fairly well.

The module comes with four optically-isolated FET open-drain (aka “NPN”) power-driver outputs or relays, depending on the version you choose. Outputs are labelled Y0 through Y3 and are assigned to Arduino pins 9 – 12. The 10MR has relays and the 10MT has transistors. There are also 6 digital inputs and 3 analog inputs and a 2-line, 16 character LCD. The units specify a 24VDC power supply, but I have successfully run them from 12V also.

Overview

So far I’ve only used the outputs on the 10MR and aside from a momentary surprise — the outputs are fully isolated, so you need to supply a ground and power to the load, it worked immediately to drive my test solenoid. Since it’s Arduino powered, it’s easy to control using the standard Arduino API commands.

Having a display is a really nice plus, but it would be even better if there were a few pushbuttons. However, those are easy enough to add with the many digital inputs. I put together a demo platform with a power supply and a pushbutton mounted to a short piece of DIN rail and the PLC screwed directly to a panel. For output, I have a small stacklight that I converted from incandescent lighting to LED.

I ran a test with a basic one-shot timer having a pushbutton input on X0 and an output on Y0. Here’s the code.

// Project sponsor: N/A
// Email: sales@cedarlakeinstruments.com
// Creator: Cedar Lake Instruments LLC
// Date: January 2023
//
// Description:
// Demonstrate one-shot logic with Arduino
// Tested on ZanhorDuino board
// 
// Arduino pins
// 9 - Digital trigger input. Pull LOW to trigger one shot
// 8 - One shot output (goes HIGH for one shot time)
//
//
// Test program for ZanhorDuino PLC
// NPN outputs are optically isolated so need their own V+ & GND connections
//

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

// set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27,16,2);  

// ********** U S E R  A D J U S T M E N T S ********************
// One shot time in milliseconds
const int DELAY_MS = 6000;
// **************************************************************

const int INTERVAL_MS = 100;
int _timeout = 0;
const bool OUTPUT_ON = false;
const bool OUTPUT_OFF = true;

// X0 is input
const int TRIGGER_PIN = 8;

// Y0 is output
const int OUT_PIN = 9 ;


void setup() 
{
    pinMode(TRIGGER_PIN, INPUT_PULLUP);
    pinMode(OUT_PIN, OUTPUT);
    digitalWrite(OUT_PIN, OUTPUT_OFF);

    lcd.init();
    // Print a message to the LCD.
    lcd.backlight();
    printStatus(DELAY_MS, LOW);   
}

void loop() 
{
    // One shot triggers on high->low transition of TRIGGER pin
    while (digitalRead(TRIGGER_PIN) == HIGH)
    {
        // Hold while pin not pressed
    } 

    // Activate output and start timing
    _timeout = DELAY_MS;
    digitalWrite(OUT_PIN, OUTPUT_ON);   

    // Hold output High until timeout 
    while ((_timeout-= INTERVAL_MS) > 0)
    {
        printStatus(_timeout, HIGH);
        delay(INTERVAL_MS);
    }

    // Turn output off
    digitalWrite(OUT_PIN, OUTPUT_OFF);
    // Update display
    printStatus(DELAY_MS, LOW);
}

// Prints remaining time and output status
void printStatus(int t, bool state)
{
    char buffer[17];
    // Pad to 16 characters: 12 for text (left-justified) 
    // and 4 for time (right-justified)
    sprintf(buffer,"%-12s%4d","TIME:",t/1000);
    lcd.setCursor(0,0);
    lcd.print(buffer);
    
    lcd.setCursor(0,1);
    state ? lcd.print("Output ON ") : lcd.print("Output OFF");
}

This works nicely. Press the pushbutton and the output turns on and the display shows a countdown in seconds until the output is back off.

When I have some more time, I’ll take a look at the analog inputs!




If you'd like to subscribe to this blog, please click here.

Categories
Uncategorized

What is an Embedded System and what does Arduino have to do with it?

An Embedded Computer System is a small computer that is “embedded” inside something else. Unlike a laptop computer, or a desktop, or a server, its primary purpose isn’t processing data, but it is meant to control some aspect(s) of the “thing” it’s embedded into. You probably don’t think of your clothes washing machine as a computer, but if it was built in the last 10 years, it almost certainly has an embedded computer controlling it. Likewise, the car or bus that takes you to work has dozens of embedded computers doing tasks as complex as sequencing the operation of the engine, or as basic as raising and lowering the windows.

How big are they?

A typical mainstream CPU (Central Processing Unit: the heart of the computer) such as an AMD Ryzen runs at a clock speed of almost 4GHz, requires more than 65W of power and has over 900 pins for external connections.

By contrast, the majority of embedded systems are based on microcontrollers. A microcontroller is a single package that houses a CPU, perhaps a basic clock source, some RAM memory to hold data and Flash storage for the program that it will execute, and peripherals that can read inputs like switches and pushbuttons, measure temperatures and voltages, and control output devices.

A basic AVR Mega microcontroller that might be found controlling the functions in your washer will run on substantially less than 1W of power, has a nominal clock speed of 8Mhz (but can run much slower to save power) and might require fewer than 20 pins to control such things as water flow, agitator cycling or spinning at the end of cycle. The microcontroller in your toothbrush might only have 6 pins and could run for years on a single AAA cell if necessary.

At the other end of the spectrum, more modern 32-bit microcontrollers have 1M+ of Flash memory and 256kb of RAM. Some microcontrollers can access external memory , further expanding their usefulness.

Why use them?

The benefit that an embedded system has over using logic “chip” to do their operations as was more popular in the past is primarily that a single low-cost chip can replace potentially dozens of external chips, thereby saving space, power and, of course, money. Additionally, a manufacturer may base a number of their products on a single microcontroller type, thereby saving on inventory costs and training. Since microcontrollers are computers at their heart, they can be reprogrammed to perform functions specific to the product they are embedded into.

Where does Arduino come in?

The Arduino is a basic microcontroller platform that was initially intended to offer artists and hobbyists a simple way to get involved in embedded systems. It has expanded from its basic roots to become a huge Open Source ecosystem of programming tools, libraries that make it easy to program unique functionality and add-on hardware that offers motion control, temperature and humidity measurement, multicolor LED displays, sophisticated graphics and so on.

Arduino continues to be a great way to get your feet wet in programming embedded systems.




If you'd like to subscribe to this blog, please click here.

Categories
Uncategorized

Run .NET programs on Raspberry Pi

What was once unthinkable has now happened: you can run software written for Microsoft’s .NET framework on Linux. This means you can now write code in Visual Studio on a PC that can be executed on a pi running Linux.

The process is pretty simple. Let’s start by assuming you have Visual Studio (at least the 2017 version) and a current version of the .NET SDK on your host computer. You can download that from Microsoft here.

Open a new command shell and create a directory called blinky.

Go to the directory: cd blinky.

Type the command dotnet new console

This creates a Visual Studio project called blinky. Replace the Program.cs file with our sample code (TBD) to blink an LED.

Build the program for Linux with the command dotnet publish -r linux-arm

We now have an application package in the publish subdirectory that we can load onto a Raspberry Pi running Linux. Let’s get the pi setup to run .NET Core. We’re going to assume that it’s running Raspbian Debian 9 Jessie since that’s current as this is written.

sudo apt-get install curl libunwind8 gettext apt-transport-https

sudo apt-get update

After the .NET Core framework has been installed, copy the publish directory from your PC to the pi and execute the blinky.exe program to run it. Enjoy your blinking LED!

Here is the source code of the blinky Program.cs

using System;
using System.Threading;
using System.Device.Gpio;

namespace blinky
{
    // Blinks an LED on GPIO pin 16 on a Raspberry pi
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            int pin = 16;
            GpioController controller = new GpioController();
            controller.OpenPin(pin, PinMode.Output);

            int lightTimeInMilliseconds = 1000;
            int dimTimeInMilliseconds = 200;

            while (true)
            {
                Console.WriteLine($"Light for {lightTimeInMilliseconds}ms");
                controller.Write(pin, PinValue.High);
                Thread.Sleep(lightTimeInMilliseconds);
                Console.WriteLine($"Dim for {dimTimeInMilliseconds}ms");
                controller.Write(pin, PinValue.Low);
                Thread.Sleep(dimTimeInMilliseconds);
            }
        }
    }
}



If you'd like to subscribe to this blog, please click here.

Categories
Arduino

Your first Arduino program

So you’ve been hearing about this Arduino thing and you made the jump and downloaded the IDE since it’s a good first step and doesn’t cost anything. Now what?

Well, first of all, what’s an “IDE?” An IDE is an abbreviation for Integrated Development Environment. To write code you need a text editor and to program the Arduino device, you need a programmer. The “I” in Integrated means that the application contains both an editor and a programmer. The Arduino IDE is a very basic one but it gets the job done.

So, what’s a “sketch?” A sketch is the Arduino terminology for a computer program, which is the set of instructions you give a computer to tell it to do something. We also call this “code.” Why the Arduino inventors called a program a sketch I have no idea, but it seems to have stuck.

void setup()
{
}

The lines above are familiar to anyone who’s written an Arduino sketch. The basic outline of a sketch has two of these blocks called “functions.” A function is a short block of code that can be called, or told to execute from anywhere in your program. The other basic function is “loop” and we’ll get to it later. For now, the important thing to know is that setup is called when the program starts and loop is called repeatedly, over and over.

Since setup is called at the beginning, it’s the place where we put statements that need to be executed as soon as the Arduino starts up. e.g., we need to tell it which pins we want to use as inputs and outputs, what special configurations they need, and so on. Here’s a basic program that makes pins 2&3 outputs and blinks them five times.

void setup(void)
{
    // Set pins 2 and 3 as outputs
    pinMode(2, OUTPUT);
    pinMode(3, OUTPUT);

    // Declare a variable for counting
    int i = 0;
    // Turn pins 2 & 3 off 5 times
    // using a for... loop
    for( i = 0; i < 5; i++ )
    {
        // Turn OFF pin 2
        digitalWrite(2, LOW);
        // Turn ON pin 3
        digitalWrite(3, HIGH);
        // Pause for 1000 milliseconds (1 second)
        delay(1000);

        // Turn ON pin 2
        digitalWrite(2, HIGH);
        // Turn OFF pin 3
        digitalWrite(3, LOW);
        // Pause for 1000 milliseconds (1 second)
        delay(1000);
    }
    // Turn OFF pin 2
    digitalWrite(2, LOW);
}

The short block of code above shows something important that’s often ignored: you can do real work in the setup() function. In fact, for some very short programs that just need to do a task and stop, it’s a good way to write your code.




If you'd like to subscribe to this blog, please click here.