SignA5.com

Brett lives here…

4×4 RGB Matrix

TAGS: None

Since I have pretty much received all my ordered items from China and surrounding countries of electronic cheapness, I thought now would be a good time to sit down and have a play with some of it.  Since I received 100 RGB LEDs (and free resistors… SCORE!) and had them sitting there; begging for me to actually do something, I figured for some fun I should make an RGB matrix.  Simple right?  Well surprisingly yes, it was.  Intiailly I wanted to create a nice 8×8 matrix, but due to me running out of jumper wires, I decided to make it 4×4.  From there the next hurdle was actually coding it.  Since I had no idea how, I spent around a week scouring the web finding code that would work with an 8×8 matrix.  Eventually I found something, which I butchered to be compatible.  The end result was definitely a plus to my already dismal week.

Pictures:


Video of it in Action:

Now you’ve seen it, how does it work?  You’ll notice I have only used 16 connections… to control every possible colour of the rainbow (well less because of certain restraints… but you know…) of 16RGB LEDs (or 48 LEDs counting each lead).  To achieve this I used a method known as a Multiplexed Display.  As I go onto talk more about it, have a quick look at the following diagram that I will reference to soon…

How I wired the RGB matrix

How I wired the RGB matrix.

Multiplexed Displays are electronic displays where the entire display is not driven at one time. Instead, sub-units of the display (typically, rows or columns for a dot matrix display or individual characters for a character orientated display, occasionally individual display elements) are multiplexed, that is, driven one at a time, but the electronics and the persistence of vision combine to make the viewer believe the entire display is continuously active.

Take note of how the rows are all connected and each of the columns (RGB) are also connected.  Each row is controlled by each of the 4 LED’s in that row’s common cathode being connected to 1 pin on the shift register.  Then each column’s RGB is connected and controlled.  Basically we use the “cordinates” to turn the LED we want on.  so- to make the second LED in the second row (2, 2) red or blue or green (or a mixture)… I would give the Cathodes on row 2 5V and then make the red or blue or green pins sink the current.  Do this repeatedly and faster for other LEDs, we can trick the eye into seeing only one image.  Cool huh?  Thats really as far as I want to go into for how it works…  If you ever need to know more, Google is amazing for that…

Selecting the 2nd LED in Row 2 via the columns and rows.

Selecting the 2nd LED in Row 2 via the columns and rows.

Soon my 8×8 RGB LED board should arrive in so I can play with that and do something more advanced… (I’ll give you a hint… It involves using BassLib…)  But until next time… :)

Long time…

TAGS: None

Eukalyptus and I have been doing some work on a help file for Bass Lib. So far it is going well… I haven’t been doing much work with Arduino for a while because of it, but I do have a few side projects that I’ve been working on that are related… So far a small power supply and I have been playing around with a home made RGB matrix (4×4 LEDs) connected to some shift registers (74HC595). I’m still working it all out, so we shall see how I end up going with those. So far the shift register half works. The code is just a tad challenging. No doubt I will work my way around it… I hope. So yeah. Once the help file is out of the way I’ll probably be back at university, so there will be less time to muck around with it all :( . We shall see what happens…

Controlling an Arduino with AutoIt – LEDs Part 2

TAGS: None

So you probably saw my last post where I controlled just two LEDs using AutoIt and my Arduino. After a few hours sleeps there were a few problems I identified in the code on the Arduino, one being how easy it was to expand. Basically the problem was if I wanted to control 10 LEDs? Obviously I am not going to go through the time consuming task of hard coding one after the other, so a better solution had to be found.
What I discovered was arrays made it a lot easier to increase the number of LEDs controllable by the code, just by changing a few lines. Now I will show you how I increased the capability of my code to 5LEDs. First step was to create the circuit. Here I have wired 5 LEDs with resistors to digital pins 9 to 13. I have used a common ground. Cue diagrams and schematics.

Breadboard Diagram

How I wired my bread board with the LEDs.

And as before test it, this time with a modified sketch:

?View Code ARDUINO
/* Sketch Name..: Serial LED Test
 * Author.......: Brett Francis
 * Website......: http://www.signa5.com
 * Description..: Turn a LED on a pin either on or off.  Used as a test
 *                for connections so we can use the serial comunication
 *                sketch.
 * Date Created.: 11/06/2010
 * Modifed......: -
 */
 
//LED Pins
#define PIN_MAX 5
int pinLED[PIN_MAX] = {13, 12, 11, 10, 9};
 
//Initial Stuff
void setup ()
{
  //Set the LED pins to be an OUTPUT
  for( int i=0; i < PIN_MAX; i++)
  {
	pinMode (pinLED[i], OUTPUT);
  }
}
 
// Loop
void loop ()
{
  for( int i=0; i <  PIN_MAX; i++)
  {
    pinON (pinLED[i]);
    delay(1000);
  }
}
 
void pinON (int pin)
{
  //Turn all other LEDs off
  for(int x=0; x <  PIN_MAX; x++)
  {
    digitalWrite(pinLED[x], LOW);
  }
  //Turn our LED on
  digitalWrite(pin, HIGH);
}

And some pictures of the final product while testing.

LEDs

All the LEDs are working!

Breadboard

This is how I wired the bread board.

So now we know it works here is the Arduino sketch. Pretty much the same as last time but with some small changes It has been slightly modified to allow for the array (like we did in the test). With our current protocol it means we can control up to 10 separate LEDs. With a little more work you could increase it to more or even

?View Code ARDUINO
/* Sketch Name..: LED Control using Serial Communication - Expandable
 * Author.......: Brett Francis
 * Website......: http://www.signa5.com
 * Description..: Turn a LED pin either on or off using an AutoIt program
 *                comunicating using a serial connection.
 *                This version is expanded, and allows a "limitless" number
 *                of LEDs to be used.
 * Date Created.: 12/06/2010
 * Modifed......: -
 */
 
// Include Function Library
#include 
 
//LED Pins
#define PIN_MAX 5
int pinLED[PIN_MAX] = {13, 12, 11, 10, 9};
 
//EOFChar
char myEOF = 13;
 
// Create the input string
String inString = String(4);
 
void setup ()
{
  //Set the LED pins to be an OUTPUT
  for( int i=0; i < PIN_MAX; i++)   { 	pinMode (pinLED[i], OUTPUT);   }   //Start Serial Communication   Serial.begin(9600);   //Print a message to the user   Serial.println("Serial Communication Ready");   Serial.print("Max LEDs Availible: ");   Serial.println(PIN_MAX); } // Loop Through void loop () {   if(Serial.available() > 0)
  {
    IncomingChars();
  }
}
 
void IncomingChars() {
  // read the incoming data as a char:
  char inChar = Serial.read();
  // if you're not at the end of the string, append
  // the incoming character:
  if (inChar == myEOF)
  {
    Serial.print("Got String: ");
    Serial.println(inString);
    char str1 = inString.charAt(0);
    int curpin = str1 - '0';
    Serial.print("Got Pin: ");
    Serial.println(curpin);
    char str2 = inString.charAt(1);
    int curstate = str2 - '0';
    Serial.print("Got State: ");
    Serial.println(curstate);
 
    if (curpin < PIN_MAX)
    {
      curpin = pinLED[curpin];
      if (curstate == 1)
      {
        curstate = HIGH;
      }
      else
      {
        curstate = LOW;
      }
      Serial.print("Turing LED on pin ");
      Serial.print(curpin);
      Serial.print(" to state ");
      Serial.println(curstate);
      digitalWrite(curpin, curstate);
    }
    else
    {
      Serial.println("LED number out of range!");
    }
    inString = "";
  }
  else
  {
    inString.append(inChar);
  }
}

To control it I just modified the blink example script to work with multiple LEDs. Just like before it requires the Serial COM UDF.

?View Code AUTOIT
;Include the Serial UDF
#include 'CommMG.au3'
 
;Internal for the Serial UDF
Global $sportSetError = ''
 
;COM Vars
Global $CMPort = 6				; Port
Global $CmBoBaud = 9600			; Baud
Global $CmboDataBits =  8		; Data Bits
Global $CmBoParity = "none"		; Parity
Global $CmBoStop = 1			; Stop
Global $setflow = 2				; Flow
 
$numberleds = 5
;Start up communication with the Arduino
_CommSetPort($CMPort, $sportSetError, $CmBoBaud, $CmboDataBits, $CmBoParity, $CmBoStop, $setflow)
 
;Start a loop
While 1
	For $i = 0 to $numberleds
		_CommSendstring($i & "1" & @CR)
		Sleep (500)
	Next
	For $i = 0 to $numberleds
		_CommSendstring($i & "0" & @CR)
		Sleep (500)
	Next
WEnd

Here is a quick video of it in action.

So that is it really… next step for me is to play with my LCD which came in the mail today. Stay tuned for that one.

And as before all files are available for download here.

  • Author: Brett
  • Published: Jun 11th, 2010
  • Category: Arduino
  • Comments: 1

Controlling an Arduino with AutoIt – LEDs

TAGS: None

As many of you know, I am a nerd. My most recent nerdy acquisition was an Arduino. For those that have no idea what an Arduino is or does, let me give you a quick explanation- straight from the Arduino website:

Arduino is a tool for making computers that can sense and control more of the physical world than your desktop computer. It’s an open-source physical computing platform based on a simple microcontroller board, and a development environment for writing software for the board.

Arduino can be used to develop interactive objects, taking inputs from a variety of switches or sensors, and controlling a variety of lights, motors, and other physical outputs. Arduino projects can be stand-alone, or they can be communicate with software running on your computer (e.g. Flash, Processing, MaxMSP [or in our case, AutoIt].) The boards can be assembled by hand or purchased preassembled; the open-source IDE can be downloaded for free.
The Arduino programming language is an implementation of Wiring, a similar physical computing platform, which is based on the Processing multimedia programming environment.

http://www.arduino.cc/en/Guide/Introduction

When I first discovered Arduino I was delighted as it had some great features – features that I was looking for. First things first, it was cheap. Very cheap. For a starter kit (Includes the Duemilanove version of the Arduino, USB cable, breadboard, jumpers, assortment of components from resistors to LEDs to switches) cost me AU $59.95. For the basic Arduino alone, you can expect to pay around AU $40. For the features and power this board has, it is a great bargain. Along with my starter kit I bought a 100 Common Anode RGB LEDs, a larger breadboard and various other components to get enough gear to have a proper play.

After getting over the first hurdle of getting the basic Arudino tutorials up and running and having a general play, I wanted to move onto my next challenge- making it work with AutoIt. If you didn’t know AutoIt is my current language of choice; it was my first language and to be honest, I’m too lazy to learn anything “better”, keeping in mind that for an interpreted scripting language it is very powerful indeed- more than enough functionality for what I use it for. And for my basic projects it was perfect.

The first thing to do was to work out how to communicate with the Arduino and my PC. Behold the first step, the serial communication. The best example I have found about serial communication is below:

Serial may sound like a tasty breakfast food, but its actually quite different. The word serial means “one after the other.” For example, a serial killer doesn’t stop with one murder, but stabs many people one after the other. Serial data transfer is when we transfer data one bit at a time, one right after the other.

Information is passed back & forth between the computer and Arduino by, essentially, setting a pin high or low. One side sets the pin and the other reads it. It’s a little like Morse code, where you can use dits and dahs to send messages by telegram. In this case, instead of a long cable, its only a few feet.

http://www.ladyada.net/learn/arduino/lesson4.html

Or a picture for those visually inclined…Serial Comunication Diagram

Serial communication in the Arduino is done using the Serial Library (who would have guessed it!)
Serial Library Diagram

The library is easy and simple to use, and it includes a set of functions to communicate between your computer and the Arduino:

Serial.Begin()
Serial.end()
Serial.available()
Serial.read()
Serial.flush()
Serial.print()
Serial.println()
Serial.write()

The main functions we will use is Serial.Begin(), as it is required to start the serial communication; Serial.Available(), so we know when data is being sent to the Arduino; Serial.Read() and Serial.Write() for directly reading data. So how can we say, control a LED with Autoit.

To start lets define what we want to achieve. At the end of this project want to be able to turn on 2 LEDs, a red and a yellow one. First lets hook our LEDs to our breadboard and Arduino. In this case I will use pins 12 and 13, placing an appropriate resistor on pin 12 as pin 13 already has one inbuilt. Here is some pictures of the final product:

LEDs

How I wired the LEDs.

The Arduino

How I wired the Arduino

And just incase you have no idea what I have done here is a diagram created using Frizting. Frizting makes it very easy to create diagrams so it is worth trying it out if you haven’t used it already.

Wiring Diagram

This is how I wired the Arduino to the breadboard.

Now let us make sure that we’ve connected everything up correctly. To do that I devised a simple test based on the blinking example; flash one LED then the other LED… Simple!

?View Code ARDUINO
/* Sketch Name..: Serial LED Test
 * Author.......: Brett Francis
 * Website......: http://www.signa5.com
 * Description..: Turn a LED on a pin either on or off.  Used as a test
 *                for connections so we can use the serial comunication
 *                sketch.
 * Date Created.: 11/06/2010
 * Modifed......: -
 */
 
//LED Pins
int yelLED = 13; //Inbuild resistor
int redLED = 12;
 
void setup ()
{
  //Set the LED pins to be an OUTPUT
  pinMode (yelLED, OUTPUT);
  pinMode (redLED, OUTPUT);
}
 
// Loop
void loop ()
{
  //Turn yellow on, red off
  digitalWrite(yelLED, HIGH);
  digitalWrite(redLED, LOW);
  //wait 1 second
  delay(1000);
  //Turn yellow off, red on
  digitalWrite(yelLED, LOW);
  digitalWrite(redLED, HIGH);
  //wait 1 second
  delay(1000);
}

So now we have that working, lets work on using serial communication. I decided that I would create a simple protocol to turn a specific LED on and then off.

LSCR

L is an integer translating to which LED to control. In this case, 1 is for the red LED and 2 is for the yellow.
S is another integer which dictates the state- 1 sets the state of the LED to on, or 0 to turn it off. The CR is a carriage return, so the Arduino knows when we have stopped sending data.

Lets start creating the sketch that will take commands from our AutoIt program. To manipulate the received data, I will be using the string library, formally known as TextString.

?View Code ARDUINO
/* Sketch Name..: LED Control using Serial Communication
 * Author.......: Brett Francis
 * Website......: http://www.signa5.com
 * Description..: Turn a LED pin either on or off using an AutoIt program
 *                comunicating using a serial connection.
 * Date Created.: 11/06/2010
 * Modifed......: -
 */
 
// Include Function Library
#include 
 
//LED Pins
int yelLED = 13; //Inbuild resistor
int redLED = 12;
 
//Internal
int curpin;
int curstate;
 
//Lets the function know if we have a string to display
int stringsent = false;
 
//EOFChar
char myEOF = 13;
 
// Create the input string
String inString = String(4);
 
void setup ()
{
  //Set the LED pins to be an OUTPUT
  pinMode (yelLED, OUTPUT);
  pinMode (redLED, OUTPUT);
  //Start Serial Communication
  Serial.begin(9600);
  //Print a message to the user
  Serial.println("Serial Communication Ready");
}
 
// Loop Through
void loop ()
{
  if(Serial.available() > 0)
  {
    getIncomingChars();
  }
 
  if (stringsent == true)
  {
    switchLED();
    inString = "";
  }
}
 
//Function to turn LED on or off
void switchLED()
{
  //We know we have a string now
  stringsent = false;
 
  //PROTOCOL
  //   LS = LED (1 CHAR) + STATE (1 CHAR)
 
  //Trick to convert a string directly to a number
  char str1 = inString.charAt(0);
  curpin = str1 - '0';
  char str2 = inString.charAt(1);
  curstate = str2 - '0';
  //Convert the state 0, 1 to something more usable.
  if (curstate == 1)
  {
    curstate = HIGH;
  }
  else
  {
    curstate = LOW;
  }
 
  // Turn the relevant LED on or OFF
  if (curpin == 1)
  {
    digitalWrite(redLED, curstate);
  }
  else if (curpin == 2)
  {
    digitalWrite(yelLED, curstate);
  }
}
 
void getIncomingChars() {
  // read the incoming data as a char:
  char inChar = Serial.read();
  // if you're not at the end of the string, append
  // the incoming character:
  if (inChar == myEOF)
  {
    stringsent = true;
  }
  else
  {
    inString.append(inChar);
  }
}

Hopefully the above code should be able to able to explain what is happening but here is the basics:

The setup() function initiates serial communication, sets the LED pins to be outputs (opposed to inputs). The next bit (the loop) will loop through, see if we are getting data and if so, return data. The function that gets the data is getIncomingChars(). It basically loops through the input buffer until it hits the CR, where it will return that value and set a variable to true. It is by setting the variable to true that we know we have to turn a light on or off which leads to the last function, switchLED(). It takes the input string and uses the string functions to extract the first (LED number) and second (state) from the string. Then it turns the state into a value the digital functions can understand, and turns the relevant LED on or off. Pretty simple eh?

Now to the AutoIt code. It is obvious from how we are communicating with the Arduino that we need some sort of serial UDF. Thankfully martin has come to the rescue with his UDF to communicate with the serial port (and in our case a USB serial port). If you play with the example you can send 11, 10 to turn the red LED on or off. So we know that part works lets create our own code. What we will create first is something similar to our test, and then I’ll add some additional scripts. Really what you can do is only limited by your imagination.

With all of the code examples, the Arduino was connected to COM Port 6. If you can’t figure out what it should be, open the plug your Arduino in and open the Arduino developer program. From there take a look at the ports listed in the Tools > Serial Port menu. If you find more than one, note the ports, disconnect your Arduino and look again. The one that is missing should be your Arduino.

?View Code AUTOIT
;Include the Serial UDF
#include 'CommMG.au3'
 
;Internal for the Serial UDF
Global $sportSetError = ''
 
;COM Vars
Global $CMPort = 6				; Port
Global $CmBoBaud = 9600			; Baud
Global $CmboDataBits =  8		; Data Bits
Global $CmBoParity = "none"		; Parity
Global $CmBoStop = 1			; Stop
Global $setflow = 2				; Flow
 
;Start up communication with the Arduino
_CommSetPort($CMPort, $sportSetError, $CmBoBaud, $CmboDataBits, $CmBoParity, $CmBoStop, $setflow)
 
;Start a loop
While 1
	;Turn yellow on and red off
	_CommSendstring("21" & @CR)
	_CommSendstring("10" & @CR)
	;Wait a second
	Sleep (1000)
	;Turn yellow off and red on
	_CommSendstring("20" & @CR)
	_CommSendstring("11" & @CR)
	Sleep (1000)
WEnd

Here are a few different additional examples as promised.

Police Style Strobe
A strobe similar to those you see on emergency service vehicles and the like.

?View Code AUTOIT
;Include the Serial UDF
#include 'CommMG.au3'
 
; What LED are we using?
Global $light = 1
 
;Internal for the Serial UDF
Global $sportSetError = ''
 
;COM Vars
Global $CMPort = 6 ; Port
Global $CmBoBaud = 9600 ; Baud
Global $CmboDataBits = 8 ; Data Bits
Global $CmBoParity = "none" ; Parity
Global $CmBoStop = 1 ; Stop
Global $setflow = 2 ; Flow
 
;Start up communication with the Arduino
_CommSetPort($CMPort, $sportSetError, $CmBoBaud, $CmboDataBits, $CmBoParity, $CmBoStop, $setflow)
 
;Start a timer
$timer = TimerInit()
 
;Start a loop
While 1
	;Check if we should change the LED
	If TimerDiff($timer) >= 750 Then
		;Turn the current one off so it doesn't stay light
		_CommSendstring($light & "0" & @CR)
		;Change the light
		If $light = 1 Then
			$light = 2
		Else
			$light = 1
		EndIf
		;Reset the timer
		$timer = TimerInit()
	EndIf
	;Turn it off
	_CommSendstring($light & "0" & @CR)
	;Wait 50ms
	Sleep(50)
	;Turn it on
	_CommSendstring($light & "1" & @CR)
WEnd

Countdown Clock
What I like to call “Countdown Clock” – the strobe gets faster as the seconds progress while the other LED flashes every second. A little bit of fun really… It is also my favourite examples…

?View Code AUTOIT
;Include the Serial UDF
#include 'CommMG.au3'
 
;Value for the red to stay off for (ms)
$waitoff = 900
 
;Internal Timing
$waiton = 1000 - $waitoff
$wait = $waitoff
$oldsec = @SEC
$freq = _GetFreq()
 
;LED State
$redon = False
$yelon = False
 
;Internal for the Serial UDF
Global $sportSetError = ''
 
;COM Vars
Global $CMPort = 6 ; Port
Global $CmBoBaud = 9600 ; Baud
Global $CmboDataBits = 8 ; Data Bits
Global $CmBoParity = "none" ; Parity
Global $CmBoStop = 1 ; Stop
Global $setflow = 2 ; Flow
 
;Start up communication with the Arduino
_CommSetPort($CMPort, $sportSetError, $CmBoBaud, $CmboDataBits, $CmBoParity, $CmBoStop, $setflow)
 
; Start a timer
$timer = TimerInit()
$timer2 = TimerInit()
 
;Start a loop
While 1
	;So we don't get the frequency every 2ms...
	If $oldsec <> @SEC Then $freq = _GetFreq()
 
	;Make the red LED flash once a second.
	;It is using an alternative timing for on and off states
	If TimerDiff($timer) >= $wait Then
		If $redon = True Then ;LED IS ON- TURN IT OFF
			$redon = False
			$wait = $waitoff
			_CommSendstring("10" & @CR)
		Else
			$redon = True
			$wait = $waiton
			_CommSendstring("11" & @CR)
		EndIf
		$timer = TimerInit()
	EndIf
 
	;Flash the Yellow LED at the correct frequency.
	;Frequency is caclulated with _GetFreq every second.
	;The higher the seconds, the higher the freqency.
	If TimerDiff($timer2) >= $freq Then
		If $yelon = True Then
			$yelon = False
			_CommSendstring("20" & @CR)
		Else
			$yelon = True
			_CommSendstring("21" & @CR)
		EndIf
		$timer2 = TimerInit()
	EndIf
WEnd
 
Func _GetFreq()
	;This function takes the current seconds count
	;and converts it to a frequency.
	$oldsec = @SEC
	Select
		Case $oldsec > 55
			Return 20
		Case $oldsec > 50
			Return 50
		Case $oldsec > 40
			Return 100
		Case $oldsec > 30
			Return 200
		Case $oldsec > 20
			Return 300
		Case $oldsec > 10
			Return 400
		Case Else
			Return 500
	EndSelect
EndFunc   ;==>_GetFreq

So for now thats it. This was a huge post, but there was a lot to cover… Later I will explore playing with more LEDs and maybe even PWM and RGB LEDs! Oh this is so much fun :D

All code files are available for download:

Code Files

Website active

TAGS: None

So it has been around 6 months since I had done anything on my site.  Not a lot has been happening, so there is nothing to report on. Ohhhhh and this is also a test! :)

© 2009 SignA5.com. All Rights Reserved.

This blog is powered by Wordpress and Magatheme by Bryan Helmig.