Using Arduino as an input trigger to PowerShell

I have previously blogged about using the Arduino as an I/O device to set the output pins to drive camera tally lights.

PowerShell can also read the input status of pins on the Arduino using the Solid.Arduino .Net library.

This is an example of using the Arduino to detect the monitor switch on camera RCP units so that you can switch the signal to a scope or monitor.

I have used this for both Sony RCP-1500 and Panasonic AK-HRP200 RCP’s which provide dry contact closures on monitor selection.

The code is very simple and sets up an event to wait for input pin status changes on the Arduino…


add-type -path '.\Documents\WindowsPowerShell\Solid.Arduino.dll'
$connection = New-Object Solid.Arduino.SerialConnection("COM4",[Solid.Arduino.SerialBaudRate]::Bps_57600)
$session = New-Object Solid.Arduino.ArduinoSession($connection, 2000)
$session.SetDigitalPinMode(4,[Solid.Arduino.Firmata.PinMode]::DigitalInput)
#
# Define the function that is called when a digitial pin event happens
#
function inputevent($event){
    #Write-Host "Something happened on port $($event.value.port) Type $($event.value.pins)"
    if($event.value.pins -band 4) #using binary AND incase multiple inputs are activated
    {
        # put code here to switch ATEM Aux or VideoHub routing
        Write-host "Cam 1 preview selected"
    }
}
#
# set up the event to montor digital pin state change
#
$session.SetDigitalReportMode(0,$true) #enable events for pins on port 0
Unregister-Event eventBitChange -ErrorAction SilentlyContinue #incase we are re-running the script
$ArduinoEvent = Register-ObjectEvent -InputObject $session -EventName DigitalStateReceived -SourceIdentifier eventBitChange -Action {inputevent($eventArgs)}

Control Arduino GPIO with Windows PowerShell Script

You can control an Arduino connected to Windows 10 via a USB cable with PowerShell.

Windows 10 provides automatic detection and loading of drivers for Arduino devices. By using a .Net library called Solid.Arduino (https://github.com/SolidSoils/Arduino ) we can control the Arduino IO pins from our script.

Your Arduino should have the StandardFirmata sketch installed (see blog posting https://ianmorrish.wordpress.com/2016/05/21/install-standardfirmata-on-arduino-for-gpio/ )

You can download a pre-compiled copy of Solid.Arduino from

Start PowerShell_ISE.exe and paste in the following code. Update the path to Solid.Arduino to match your location.

You can change the $LEDs array to specify the pins that you have connected LED’s to.

add-type -path ‘C:\Users\imorrish\OneDrive\PowerShell\GPIO\Solid.Arduino.dll’

$connection = New-Object Solid.Arduino.SerialConnection(“COM4”,Solid.Arduino.SerialBaudRate]::Bps_57600)

$session New-Object  Solid.Arduino.ArduinoSession($connection,2000)

Start-Sleep -Seconds 1

$session.GetFirmware()

#Turn on LED’s one by one

$sleepTime 2

[Int[]]$LEDs = 10,11,12,13

ForEach ($led in $LEDs){

 $session.SetDigitalPin($led,$true)

 Start-Sleep $sleepTime

 $session.SetDigitalPin($led,$false)

}

Install StandardFirmata on Arduino for GPIO

My Software Switcher Panel and PowerShell module for Blackmagic ATEM video mixers can use an Arduino for general purpose i/o (GPIO). This is often used for tally lamps on cameras can also be  used to control/trigger other devices that have GPIO terminals such as recording devices.
Windows 10 has built in support for Arduino USB connection (part of the Internet of Things strategy Microsoft has with universal apps).
To use this functionality, you must install the appropriate software, referred to as a sketch, on the Arduino.
Follow these steps..

Install Arduino IDE for Windows
Download from http://www.arduino.org/downloads

Check that you have the latest Firmata library
1.Open the Arduino IDE and navigate to:  Sketch > Include Library > Manage Libraries
2.Filter by “Firmata” and click on the “Firmata by Firmata Developers” item in the list of results.
3.Click the  Select version  dropdown and select the most recent version (note you can also install previous versions)
4.Click  Install .

Install Sketch
Load the general purpose sketch called StandardFirmata from the Arduino IDE in File -> Examples -> Firmata.
This will open a new IDE window with the sketch displayed.
Firmata

Use the menu Sketch -> Upload to install.
This is a one time task and your Arduino will remember this code.

Confirm the Serial Port
So that you can configure PowerShell or my software control panel, you need to confirm the serial port that Windows has mapped to the Arduino. It is often COM3 or COM4 but this can vary depending on other devices connected to your machine.

Use the Device Control Panel to confirm. It should look something like this example which shows COM7 (unplug the device to confirm which port it is if there is more than one).
SerialPort
You can now use this in your script or application configuration as required.