Jonathan Thomson's web journal

Adding Serial Control November 16, 2010

The code found in Getting Started with the TSL230R and Arduino only instructs the microcontroller to send data. By using the code snippet from Tod E. Kurt’s website listed below. We can extend TSL230R_basic_example.pde by adding a simple serial protocol that allows the microcontroller to receive commands sent from the host computer.

 

todbot Arduino Serial protocol design pattern

   // protocol is "CCaaaa", two bytes of command, four bytes of args
   if( Serial.available() >= 6 ) {  // command length is 6 bytes
      cmd0 = Serial.read();
      cmd1 = Serial.read();
      arg0 = Serial.read();
      arg1 = Serial.read();
     // ...etc...
    }

TSL230R_FreqCount.pde (view online) enables the user to set the number of output samples, adjust the TSL230R’s sensitivity and frequency scaling, and switch power to a light source.

The Arduino IDE has a built in serial monitor, which you can use for testing serial commands. However, Tod E. Kurt’s arduino-serial is smaller and has more functionality.


The command:
./arduino-serial -b 9600 -p /dev/ttyUSB0 -d 3000 -s s001 -d 100 -s f002 -d 100 -s l111 -d 100 -s t005 -d 10000 -r -d 500 -r -d 500 -r -d 500 -r -d 500 -r -d 500 -r -d 100 -s l000

Outputs one read for each -r:
read: 0

read: 796

read: 1012

read: 1012

read: 1010

read: EOT

 

This command waits three seconds for the bootloader to load the program (-d 3000) then it tells the program to set the sensitivity to 1x (s001), the frequency scaling to divide-by-2 (f002), turn the light on (l111), and transmit five samples (t005). The command then waits 10 seconds for the buffer to fill (-d 10000), reads five samples (-r -d 500 repeated five times), and finally turns the light off. The blank lines in the output are from the line feed (i.e. /n) printed after each number. The uc code uses Serial.print('\n') instead of Serial.println() and the string “EOT” so that it can communicate with code written for GNU Octave and MATLAB.

The archive TSL230R_data_acquisition.zip contains the m-files (view online: get_data, save_data, serial_open) necessary for programming the TSL230R and reading the frequency output from the ATmega uc in GNU Octave and MATLAB.

 

Please note that since the FreqCount library uses all of the ATmega’s timers, pulse width modulation as well as micro() and milli() are unavailable.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s