Objective: Learn how to manage simple input and output devices, using XOD programming. Import your first Library in XOD. Connect and operate external devices.
Requirements: Computer loaded with XOD and board driver, Rich UNO R3 board from Biomaker starter kit.
XOD provides a range of software nodes for control of input and output devices. The microcontroller on the Arduino board has a series of digital and analogue ports that can be used to read or write to simple devices. For example, in the "Getting Started" tutorial, we saw how to interface with touch-sensitive key input and LED outputs using standard XOD nodes. An increasing number of low cost devices are becoming available that are (i) capable of a wide range of calibrated measurements and sophisticated outputs, and (ii) use logic controllers with complex serial communication protocols. Specialised software nodes are required to use these device in XOD, and many of these can be found in published XOD libraries, either default or user-contributed (https://xod.io/libs/).
In this tutorial, we will first show how to connect a 16x2 character LCD display to the Rich UNO R3 multifunction board using a discrete component that has been connected to the Rich UNO R3 board via an expansion shield provided with the Biomaker Starter Kit, and drive the screen using the text-lcd-16x2-i2c node in the xod/common-hardware library.
Step 1: Connecting an extension shieldThe expansion board contains sockets for mounting standard Arduino UNO shields. This allows simple expansion of the board functions. Note also that the microcontroller ports are connected to on-board devices via a bank of DIP switches. This allows existing functions to be switched off, to avoid conflict with new connected devices. The kit of components provided in the Starter Kit include an IO Expansion Shield (supplier's website). This can be plugged in to simplify the connection of new hardware devices.
The component pack contains a liquid crystal display (LCD) capable of displaying 2 lines of 16 characters. The device is equipped with an I2C interface that allows serial communication with the device. The hardware device is connected to a 4-wire I2C port. Using leads provided in the Starter kit, be sure to correctly connect the Vcc, Gnd, SDA (data) and SCL (clock) wires between the I/O Expansion shield and the 16x2 LCD component.
tuto201 from the xodball
Scroll down the XOD user interface to find the xod/common-hardware library, open, identify the text-lcd-16x2-i2c node and drag onto the work area. The node allows setting parameters for the screen, and feeding data to the display.
The node drives a text LCD screen with a PCF8574 or PCA8574 I2C expander module. I2C is a serial communication standard that allows a controller to work with multiple devices connected on the same set of wires, or bus. Each device has a unique address. Commercially available I2C interfaces for 16x2 LCD displays usually have an address in the range 0x20-0x27 or 0x38-0x3F (these are hexadecimal values, base 16). The actual values of a component can be found in the documentation for the LCD/expander. The component provided in the Starter Kit should have an I2C Address of 0x38 (also represented as 38h).
Inputs:
ADDR: byte (I²C address of the expander chip)
BL: boolean (Backlight enable/disable)
L1: string (Text for the first line(
L2: string (Text for the second line)
UPD: pulse (Triggers new write)
Outputs:
DONE: pulse (Fires when write is done)
Adjust the settings of the XOD node text-lcd-16x2-i2c, to allow direct communication with the display. The address of the i2C device should be set at 38h using the ADDR parameter.
Text strings can be sent to each line of the display, and this can be a very useful tool for following programmes’ behaviour. In addition, XOD provides the watch node in the xod/debug library, and instances of this can be connected to the outputs of important nodes, to provide real-time output of values as a programme is run in debug mode.
tuto202 from the xodball
A potentiometer (variable resistor) is provided on the Rich UNO R3 board, connected to port A3. A simple pot node can be found in the xod/common-hardware library, dragged onto a work area in your project, assigned to port A3, and used to provide a user-controlled variable signal for input to other nodes.
The output of the pot node can be fed into a format-number node. This allows the user to set the number of significant digits, and convert the numerical value to a string for display on the LCD. The text-lcd-16x2-i2c node provides inputs for the two lines of display (L1 and L2), with up to 16 characters allowed for each line. A text-node can be used to provide a fixed text string as a label for a line of the display.
Step 5: Using the potentiometer to adjust the brightness of an LEDtuto203 from the xodball
Identify the LED component that was used in Tutorial 1. Using female-female leads from the Starter Kit, plug the 3 pins of the component into GND, VCC (5V) and any of the pins labeled PWM on the extension shield (namely, pins D3, D5, D6, D9, D10 and D11). Ideally, unplug the Rich UNO R3 board from its power source (USB cable and/or power pack) before rewiring circuits, and first connect the GND wire for devices.
PWM stands for pulse width modulation, and it provides a way for the Arduino microcontroller to produce variable average voltage outputs from digital (on-off) signals. The digital ports labelled PWM can deliver binary outputs (0 or 5V) as a series of rapid pulses. The relative length (duty cycle) of the 5V pulses will produce different average voltages, that can be used to control the output of devices like an LED - e.g. to regulate the brightness of the LED.
Add an LED node to your XOD code from the xod/common-hardware library. Connect the VAL output of a pot node to the LED LUM input. Upload the code and you should see the brightness of the LED change as you turn the knob of the potentiometer. The value should be echoed on the LCD screen, if connected.
Step 6: Building a new analog sensor nodetuto204-207 from the xodball
Take the Open-Smart light sensor from the Starter Kit and plug it into GND, 5V and pin A1, using the female-female leads provided.
This sensor essentially allows more current to flow through as it is exposed to more light. Exposed to 10 Lux, it will provide a resistance of 8-20K Ohms, and at 0 Lux, the resistance will be 1M Ohm.
There are no dedicated nodes for the photoresistor in XOD. However, it is relatively simple to create a custom node for this, using the analog-sensor node from the xod/common-hardware library.
Try plugging in an analog-sensor and a watch node to see what happens when you cover the light or expose it to your phone's flashlight.
Try now to add the analog sensor controlling the photoresistor and the potentiometer-controlled LED to the same XOD patch. Look at the brightness value when you turn the potentiometer and make the LED brighter.
You can display different informations on the same line of the LCD screen by using the join node, that will make all information in the same character string, with the separator of your choice.
tuto208-211 from the xodball
Now is the time to convert this raw analog readout into something actionable.
XOD has a few programming nodes that will allow you to display text on the LCD or implement actions on other components. Let's build a small program with the elements we have now.
Connect the VAL of the Analog Sensor to two new nodes : less and greater.
Chose two threshold values that you would consider too low or too bright. We suggest 0.3 and 0.8 respectively.
'Less' outputs a boolean TRUE value if `IN1` < `IN2`, and false otherwise.
'Greater' outputs a boolean TRUE value if `IN1` > `IN2`, and false otherwise.
Add one if-else node for each of the newly added 'lee' and 'greater' nodes.
If-else takes a Boolean condition as entry. It then outputs the true statement if the condition entered is true and outputs the false statement otherwise. Add a constant-string node and write "too low" and "too high" link them as the true conditions for the if-else nodes linked to less and greater respectively.
You can refer to the schematics as soon as the instructions become confusing.
We now have messages for when the light is too bright and not bright enough. Let's add a message when the light is neither.
Add a nor node. NOR outputs TRUE when all of its inputs are FALSE. Link as inputs the outputs of less and greater, and link the nor output to a new if-else node. write the message you want to signal that the light is neither too bright nor too low.
Time to aggregate all these messages into something that the LCD screen can display.
The concat node will do that for you. Link all the if-else outputs to a concat node, and then the concat node to the line 2 of the LCD screen.
tuto212-213 from the xodball
Now that you have effectively made a nice program, it is time to turn it into a node that you will be able to use for future programs.
We will focus on the light sensor and the cutoff value nodes displayed below. You can delete the rest by selecting them and hitting backspace.
Click on the menu button of the current patch you are working on (supposedly named "main", or "tuto212" if you are working from one of our xodballs) and click 'rename'. Name the patch "light-sensor"
Now open a new page from the project browser. Give it the name you want and try to drag and drop your newly created "light-sensor" into it.
The new node should appear but as you can see, nothing can be plugged in or out of it.
This is because the inputs and outputs have to be defined in the node for it to interact with the rest of XOD. Go back to your "light-sensor" patch and add the following patches:
- xod/patch-nodes/input-port. link it to the "port" pin of the analog sensor node. Name it PORT
- xod/patch-nodes/output-number. Link it to the initial sensor output.
- xod/patch-nodes/output-string . Link that to the output of the concat node.
- xod/patch-nodes/input-pulse. Link it to the UPD pin. Rename it UPD
- xod/patch-nodes/output-pulse. Link it to the DONE pin. Rename it DONE
- xod/patch-nodes/output-pulse. Link it to the ERR pin and rename it ERR
Go back to your new page, try adding "light-sensor" again. You can now connect it to other nodes and build more complicated programs using this as a building block!
tuto214-216 from the xodball
Plug the NeoPixel ring to the Rich UNO R3 board using GND, VCC and a digital port on the IO Epxansion shield and be careful to have the DIN plugged into the digital pin. Do not connect the DOUT.
Both awgrover and Bradzilla84 have developed libraries for the neopixels. you can add them to your XOD environment by loading the awgrover/adafruitneopixel and bradzilla84/neopixel libraries, respectively.
Import them both and take a moment to look at the examples published in both libraries. test your NeoPixel by uploading bradzilla's example-simple. Use your external power supply to power the board, as it will otherwise may not be able to supply enough power to light up all 16 LEDs.
Step 10: Control LED output with a potentiometerWe will be using Bradzilla's library as we go forward.
Try to re-create this program and see what happens and how your NeoPixel responds to turning the pot.
Now that you know how to build small programs, try to program the NeoPixel.
Make it change colours when the pot passes a certain threshold.
Change the pot for another analog sensor (like the light sensor).
Comments