Software Overview  |  Sitemap  |  Downloads  |  Developers  |  Forums
Small_clear_logo_llc

Recipe to Control a Relay Wired to an Arduino

Relay

The digital pins on an Arduino are able to function as digital outputs and control devices. Their outputs don't have a huge amount of drive current, but they have enough drive to control some relays.

In this recipe, we configure an Arduino pin as an output. If you wire a relay input to this output, and the relay's input requirements are within the capabilities of the Arduino (the Uno spec says outputs are 5V and drive up to 40mA), the relay will turn its output on and off.

Ingredients

For this recipe, you will need:

  • an Arduino (running Firmata)
  • a relay

Script

The Script is as follows:

# Controls an relay wired to an Arduino
# Requires an Arduino with a relay wired to one of its output pins.

# Get the file name of the Arduino device.
# On *nix systems, the file will be in the "/dev" directory with
# a name like "/dev/cu.usbmodem411" or "/dev/ttyUSB0".
# On Windows systems, the name will be something like "COM1", "COM2", ....
script_parameters("arduino_port")

# Get the output pin connected to the relay - default is pin 3
optional_script_parameters(relay_pin:3)


# add Arduino device
run_script("Scripts/Device/Arduino/Arduino", port_location:arduino_port, id:"arduino")

# make the pin of the relay an output port
run_script("Scripts/Device/Arduino/PinTypes/DigitalOutputs", names:"relay", pins:relay_pin, arduino_id:"arduino")

To run this Script, you need 2 parameters: the arduino_port and relay_pin parameters. The arduino_port parameter is the name of the USB port where the Arduino is plugged in. On *nix hosts, the USB port name is in the "/dev" directory. To see the devices in order of their creation, type "ls -lrt /dev" at a shell prompt and look near the bottom of the list for your Arduino's USB port. The relay_pin parameter defines the Arduino pin where the relay is located. It has a default value of 3.

As an example, for an Arduino at USB port "/dev/ttyUSB0" with a relay at pin 3, the Script parameters are:

arduino_port: "/dev/ttyUSB0"
relay_pin: 3

Running the Script

Run the Script, and then go to the Device Explorer page. Click on the "arduino" device and click on its "relay" pin. Enter either on or off (unquoted) to turn the relay on or off.

Catalina Computing, LLC.

Copyright © Catalina Computing, LLC. (2013-2017)




Page last updated: Mon Dec 2 00:03:05 2013 (UTC)