Pages

Thursday 11 January 2024

Arduino Command Interpreter

 

My First Arduino Project! 

This is my first Arduino project which I started over a month ago. My intention was to get a basic system up and running and establish rudimentary comms with a PC. I have a background in programming Zilog Z8 microcontrollers for data loggers, so getting the hang of the Atmel processor was a case of brushing off the dust on my knowledge of  these devices. Luckily libraries are available for programming the Arduino board so I didn't have to do any low level stuff as was the case with the Z8.
The project is broadly divided into two parts; the sketch which runs on  Arduino, and the application running on the PC (Leonardo Terminal). The sketch implements a simple command interpreter for the Arduino board, specifically the Leonardo and the PC application is a terminal program which allows users to enter commands which are passed over the USB virtual serial port to the Arduino board.  These commands instruct the microcontroller to set the states of digital outputs, generate and vary the duty cycle of a PWM signal, and read the state or value of digital and analog inputs.

The Sketch 

Incoming data on the USB serial port is echoed back to the remote terminal and also to a 16 x 2 LCD display if used. Each character is appended to a  command buffer. When a CR code  is received,the contents of the command buffer are parsed to extract the command and its parameters. The command is then executed. Commands are translated to the Arduino digitalRead, digitalWrite, analogRead, analogWrite, Tone and noTone functions. The sketch can be used with the Leonardoterminal VB application or any terminal program.  The syntax of commands understood by the interpreter is as follows:

DIN pin or DIN pin PU 
Read the state on a digital pin,  the optional parameter PU instructs the board  that pull up resistors should be used.  Value printed on LCD or output to serial port is 0 LOW, 1 HIGH
 
DOUT pin state 
Output to a digital pin. state is 1=HIGH, 0=LOW

AIN pin 

Read the value on the specified analog pin. Display to LCD and output to serial port

AOUT pin value

Implements PWM on the specified pin, value ranges 0-255. The frequency of the PWM  signal is 490 hertz approx

TONE pin pitch duration  

pitch is in hertz, duration is milliseconds          

NOTONE    pin

Turns off the tone on the specified pin                 


Leonardo Terminal 

The application which runs on a PC was written in Visual Basic 2008. This application is a simple terminal program which can be used for communicating with an Arduino board, or any other serial device.  Data is entered into a textbox and an adjacent textbox displays data received.  Two text boxes allow strings of data to be sent when "send" buttons are pressed.  The following functions on the form are specific to an Arduino board:

Monitor Analog Input 

Displays the value read from the specified pin. The pin is scanned every second

Monitor Digital Input 

Displays "HIGH" or "LOW" depending on the state of the specified pin. Results are updated every second

PWM output   

Enables PWM on the specified pin and the duty cycle for the PWM signal is set by the position of the wiper on the tracker bar

Commands entered into the Data Sent text box are translated to the Arduino digitalRead, digitalWrite, analogRead,  analogWrite, Tone and noTone functions.
Data can be written to an Excel file, however you must have Excel installed for this to work. I have only implemented logging of data on the chosen analog channel. Files can be appended or overwritten.
The software detects when a USB cable cable is plugged in and updates a list of available ports. When the cable is unplugged, the list is re-populated. 



Improvements 

There is a lot of room for improvement in the code. Rather than using a text based protocol for requesting and setting the states of pins, data could be sent and received in packets in binary format. Instead of using digitalRead, analogRead and other Arduino functions for getting/setting the states of pins, registers could be read directly and results sent as a block of data.
The advantage of a text based protocol however is that any terminal program can be used for checking/setting pins without the requirement to encode/decode data.
No error checking is implemented. Checksums could be added to sent and received commands/responses for basic validity checking of data.
The Leonardo Terminal application only receives and sets one pin at a time. Future versions will allow the full range of inputs/outputs on the board to be controlled. So for example the user can choose whether a pin is input or output, PWM output and if it should be logged.



Parts Required

Leonardo board from Dealextreme.com or other supplier.
DX.com don't appear to stock an official "Made in Italy" Arduino Leonardo anymore. However you can buy an "Eduino" clone version for $14.60 here:

http://dx.com/p/diy-eduino-leonardo-module-blue-black-213956?Utm_rid=55371787&Utm_source=affiliate
 
2 x 16 LCD display also from DealExtreme

The one I bought had a blue display with white characters. You can buy it here:

http://dx.com/p/16-x-2-character-lcd-display-module-with-blue-backlight-121356?Utm_rid=55371787&Utm_source=affiliate

However the backlight must be illuminated to read the characters. An alternative is this one with black characters on a yellow/green background:

http://dx.com/p/1602a-16-x-2-lines-black-character-lcd-module-w-chartreuse-yellow-backlight-dc-5v-151977?Utm_rid=55371787&Utm_source=affiliate 

You also need a strip of pin headers if you want to plug the LCD into a breadboard.

Breadboards can be bought from DX.com here:

http://dx.com/p/830-point-solderless-breadboard-118354?Utm_rid=55371787&Utm_source=affiliate

Breadboard Connections

The breadboard setup was drafted using Fritzing which can be downloaded here: http://fritzing.org/
I used an Uno board instead of a Leonardo, however the pin connections are the same.
5 volts and ground from the Leonardo feed the two top rails of the breadboard. Only 4 data lines of the LCD need to be connected to the Arduino. A 10k pot is used to adjust the contrast on the LCD display. 
Johan van Tongeren has some information and close-up photos of connecting this type of LCD here: 
http://www.dreamdealer.nl/tutorials/connecting_a_1602a_lcd_display_and_a_light_sensor_to_arduino_uno.html



This is the schematic:


You can download the sketch here:

Leonardo.ino v1.0

 

This is a screen dump of the Leonardo Terminal program




........and you can download the Leonardo Terminal VB code here. Unzip the folder into your Visual Studio projects folder.

Leonardo Terminal v1.0

Don't forget to visit my HubPages articles, http://eugbug.hubpages.com  (some likes or tweets would be appreciated!)



No comments:

Post a Comment