You are browsing the archive for Arduino.

Avatar of Jason

by Jason

Controlling servos using a joystick

December 13, 2012 in Arduino, Arduino, Hardware

As part of a larger project, adapting a midi joystick to control a robot to provide full movement with a laser turret mounted on top.

Hi, I’m Jason. You may remember me from Setting up LCD via GPIO on RasPi – Part 1 and Making a Pumpkin Sing. This time I will be adapting a midi joystick to control a robot to provide full movement but also a laser turret mounted on top.

First step is to find a midi joystick, and thanks to eBay I picked one up cheap (as no computer these days has a midi port, these can be picked up very cheap) and immediately took it apart to see how it works. It took a while to work out what goes where, as there is the X and Y axis, a switch and 2 buttons. I cut off the midi connector to create a pin using the expose wires.

Inside the joystick

Inside the joystick

Surprisingly there were only 6 wires, and using a LED and power source (provided by the trusty Arduino), I eliminated which wire was the power source (which actually turned out to be 2 wires, 1 each for X and Y, and 1 for each button). The auto-fire switch (when in the off position) lit the LED dimly and full brightness with the auto-fire on, which could prove to be a bit troublesome coding it. As it was tied directly into it’s own circuitry, it promptly came out and wired directly into a new pair of wires.

Circuit board from the joystick removed

Circuit board from the joystick removed

Most important is the X and Y control. As it worked in the same fashion as a potentiometer, providing analog values, ranging from 0 to +5v. Unfortunately the Raspberry Pi can’t read analog but the Arduino can, sporting 6 analog in pins. Taking the Arduino AnalogReadSerial example to read the range that the axis gives, to then use it to calculate the angle for the servo.

Expecting a simple read out (ranging from 0 to 1023 at each end) and a middle value of 511, but instead getting a middle value of 400, 0 from immediate left up to about a quarter leaning left and 500 at the upper limit to the right. Taking 5 readings at equal intervals the figure looked like this:

0 – 20 – 400 – 450 – 500
Instead of (using 500 as the upper):
0 – 125 – 250 – 375 – 500

This isn’t what I expected… Or wanted.

Now that I had to re-evaluate what’s happening (why I am getting an uneven figure), I tried experimenting with different circuits, swapping the ground and +5v on each terminal, adding resistors and trying to add some compensation code to even out the spread. Unfortunately nothing worked stable enough to be of any use.

Looking like an instant fix (which in real time took another 2 days of 3 hours), as a process of elimination I tried swapping the 5v for 3.3v not expecting anything miraculous to happen to be surprised with steady even figures that matched the position perfectly, (I could of cried after a simple swap (but I didn’t)) and trying it with the Y axis, again a steady stream of numbers gets reeled off in the serial monitor. At last I’m getting somewhere! :)

Deciding not to throw the joystick away and move onto something easier like quantum mechanics, I get both axis’ hooked up to the Arduino inputs, 2 servos into the digital pins (made sure they were in correctly!), modified the knob example and it springs into life! Then gluing 1 servo to the arm of the second giving full 180 degree movement, left right up and down.

Servo's mounted together with laser module attached

Servo’s mounted & laser module attached

Now all I need to do is stocking up on plenty of headache tablets and attempting to have the same control but over a distance using Wifi! ;)

To be continued…

Avatar of Tom L-G

by Tom L-G

Arduino temperature gauge

November 29, 2012 in Arduino, Arduino, Hardware

Arduino powered Temperature Gauge

This project displays the temperature on a simple LCD, from a Dallas one-wire temperature gauge. I wanted to make it a bit more complex, to have the LCD back-light adjust to ambient light via a light dependent resistor.

For this project, you’ll need:

Assembly
LCD

There are 8 pins on the LCD to connect to the Arduino. On the LCD, pins 3 – 7 (ascending) need to be connected to pins 7 – 3 (descending) on the Arduino and set to output. LCD pin 8 is the backlight. I’ve connected this to pin 11 on the LCD; the Arduino will change the output of the backlight depending on the values read from the light dependent resistor. LCD pin 1 powers the LCD via 3.3v and LCD pin 2 is the ground.

Temperature Sensor

This little sensor is pretty accurate and easy to wire. It has two modes; Passive and Active. I have it set up in active mode. The middle leg is wired to pin 2 on the Arduino and the 5v pin via a 470 ohm resistor. Leg 3 also goes to the 5v, leg 1 is ground. To number the legs, hold the flat edge facing you, the legs are then numbered 1 – 3, left to right. There are plenty of instructions out there to wire the sensor properly.

Light dependent resistor

This helps the LCD to change it’s backlight based on the ambient light. One leg goes to 5v, the other to both analogue input and to ground. The analogue input isn’t that sensitive, but can broadly tell whether or not it’s light or dark. It takes the analogue input between 0 and ~1000 and divides it by 4 to get a more appropriate output for Arduino pin11.

 

My C skills are still somewhat limited. The screen only displays the temperature as an integer, because I use a string as the display output. I couldn’t work out how to display a floating point number. So, any comments on how to improve the code wouldn’t co amiss!

I’ve attached a Fritzing image of the layout, so you can see the routing. I’m not sure how much it clarifies things! Any comments or questions, please let me know on here. Here you can download the Project Code from Github.

 

Avatar of Dan

by Dan

Tutorial five: Color intensity

November 19, 2012 in Arduino, Tutorials

This week’s tutorial is a very quick one. We are going to use a potentiometer to control the intensity of different color lights for a RGB LED. We’ll need the following:

As usual, on the left is the Fritzing sketch for this circuit; Lay it out as shown.

For this circuit we require 3 potentiometers, however we can achieve the same by using a button to cycle through the different colours.

The blow code is much like the code from last weeks, however this time it contains only one method which returns an integer. Last week it was a “Void”, which meant it did not require a return value.

int redLed = 9;
int greenLed = 10;
int blueLed = 11;

int redPot = 2;
int greenPot = 1;
int bluePot = 0;

void rgbWrite(int r, int g, int b){
  analogWrite(redLed, r);
    analogWrite(greenLed, g);
      analogWrite(blueLed, b);
      }
      int potRead(int x){
        int temp = analogRead(x);
          temp = map(temp, 0, 900, 0, 255);
            temp = constrain(temp, 0, 255);
              return temp;
              }

              void setup(){
                pinMode(redLed, OUTPUT);
                  pinMode(greenLed, OUTPUT);
                    pinMode(blueLed, OUTPUT);
                    }

                    void loop(){
                      int red = potRead(redPot);
                        int green = potRead(greenPot);
                          int blue = potRead(bluePot);
                            rgbWrite(red,green,blue);
                            }

									

We’ve just put together various things from previous exercises. Comment below if you need any code explanations. Next week we’ll learn how to use a seven segment display!

Avatar of Dan

by Dan

Tutorial Four: Colorful world!

November 6, 2012 in Arduino, tech fruits, Tutorials

In this tutorial we will cover RGB LED’s. A RGB LED works just like a standard LED, but has 2 extra pins. The pins are Red, Green, Blue and a common Cathode / Anode, but for the purpose of this tutorial we will be using a LED with a common cathode. You’ll need:

  • 1x Arduino
  • 1x Solderless BreadBoard
  • 1x Common Cathode RGB LED
  • 1x 220 Ω Resistor
  • 5x Jumper wires

A diagram of the standard layout for a RGB LED with common cathode is included to the below courtesy of Oomlout

 

The Fritzing sketch for this week is a simple circuit and should only take a few minutes to set up.

To use the RGB LED, we can simply use digitalWrite() to get some simple colours eg. red, green. blue and in-between. However to use the LED to its full Potential, we will take advantage of the analogWrite(); capabilities of the Arduino to create any color. (all colours consist of Red, Green and Blue of various degrees)

The code I’m using this week highlights the different ways to use the RGB LED. We also introduce a new programming technique known as “methods”, which are used to reference new commands within the program. In very simple terms, method is a piece of code that is called by name that is associated with an object. In most respects it is identical to a function, but it depends on the programming language.

Another new construct mentioned this week is a “for loop”; unlike a “while loop” you can initialize and increment ( i++ which means i+1) a variable (variable = int i) within the parameters.

// This code is used to fade a RGB Led up and down in various ways 
// it also incorporates methods to clean up the code

int redLed = 9; //hook pin 9 to red RGB LED 
int greenLed = 10;// 10 -> green
int blueLed = 11;//11 -> blue

// A method is declared as below;
// When its called, x = redLed and is read from the loop
void rgbHigh(int x){ 
// We use methods for sections of code that will be reused
// & to make the code easier to read / follow
   for(int i= 0; i<=255; i++){ // a for loop as described above 
     analogWrite(x, i); 
     delay(10); 
  } 
}
void rgbLow(int x){
   for(int i= 255; i>=0; i--){
     analogWrite(x, i); 
     delay(10); 
  } 
}
void rgbMix(int x, int y){
  rgbHigh(x); // Here we are calling a method within
  // a method to streamline the code even further 
  
  rgbHigh(y);
  rgbLow(x);
  rgbHigh(x);
  rgbLow(y);
  rgbLow(x);
}

void setup() {
    pinMode(redLed, OUTPUT);
    pinMode(greenLed, OUTPUT);
    pinMode(blueLed, OUTPUT);
}

void loop(){
 rgbHigh(redLed); // This is the earlier method in use 
 rgbLow(redLed); 
 // If we were not using a method, we would be repeating the same 
 // code again. 
 
 rgbHigh(greenLed);
 rgbLow(greenLed);
 rgbHigh(blueLed);
 rgbLow(blueLed);
 rgbMix(redLed, greenLed);
 rgbMix(redLed, blueLed);
 rgbMix(greenLed, blueLed);

}
									

The RGB LED is a robust component and can be used for many applications with the Arduino. Next week we will use the RGB LED with a 3 Potentiometer to build on the dimmer switch from last tutorial; and combine the knowledge we have already acquired to add a layer of complexity to the circuit.

Remember as always, share with us any projects you are working on and if you want the Fritzing files or Arduino sketch, ask below in the comments.

Avatar of Dan

by Dan

Tutorial Three: Fade

October 29, 2012 in Arduino, tech fruits, Tutorials

This week we’ll take advantage of the Arduinos analog input and output capabilities. To demonstrate this we will make an Arduino fade a LED up and down like a dimmer switch.

What you’ll need this week is:

  • 1x Arduino
  • 1x Potentiometer
  • 1x LED
  • 6x Jumper wires
  • 1x 560Ohm resistor

As usual, on the left is the Fritzing sketch of the circuit. Layout your circuit as per diagram (NOTE: the layout and shape depend on the potentiometer you have).

At this point you may be asking yourself, “What is a potentiometer? (Pot)”. A Pot is a variable resistor. A resistor limits/controls the flow of electricity that travels through it. A Pot acts similarly, but the amount of resistance is dependent on the pots position. This makes it easy to utilize it as a dial for an Arduino, and by using Arduinos analog pins to read the amount of current flowing to create an array of different circuits.

//This code is used to fade a LED up and down using a potentiometer// 

int lightPin = 0; //Used to store the value from the Pot
int ledPin = 9; //The LED is attached to Pin 9 on the Arduino board
//Pin 9 on the board is a PWM pin and can be used for digital 
//writing for LEDs 

void setup()
{
  pinMode(ledPin, OUTPUT); //sets the led pin to output
}

void loop()
{
  int lightLevel = analogRead(lightPin); 
  lightLevel = map(lightLevel, 0, 900, 0, 255);
  //This is used to keep light level within the 
  // 255 range that LEDs can handle
  lightLevel = constrain(lightLevel, 0, 255);
  analogWrite(ledPin, lightLevel); //used to fade a 
  //LED up and down  
}
									

So this week we saw how to use the analogWrite() and analogRead() functions in the Arduino IDE. These two functions are handy for various things. Using analog read you can made a line of LED’s light up in sequence, or make a LED flash at different intervals as in the example code provided by the Arduino IDE.

Remember as always, share with us any projects you are working on and if you want the Fritzing files or Arduino sketch, ask below in the comments.

Next week we’ll make things colourful with a RGB LED. Keep hacking and remember to follow us on Twitter if you haven’t already and like on Facebook.

Avatar of Dan

by Dan

Tutorial Two: Click it!

October 22, 2012 in Arduino, tech fruits, Tutorials

This week we’re going to start with human interaction. To do this we are going to build on the blink sketch from last week and add a button to switch it on and off.

What you’ll need:

  • 1x Arduino (uno)
  • 1x LED
  • 1x 560 Ohm resistor
  • 1x 10k Ohm resistor
  • 1x Tactile switch
  • 4x Jumper cables
  • 1x Solid core bread boarding wire

As before, I included a breadboard diagram made in Fritzing. Once you have the circuit prepared as on the diagram, take a second to look at it. Notice the switch and the LED are not connected on the bread board. In this circuit we are going to take advantage of Arduino’s reading capabilities to use the output from the tactile switch, to tell the LED to light up.

/*In this sketch we will make a tactile switch act as a light switch
to turn a light switch on and off */
int ledPin = 13; //The LED in this circuit is connected to pin 13
//on the Arduino 
int buttonPin = 3; //The tactile switch is connected to pin 3 
boolean led = true; //A boolean variable of either true or false

void setup() {
    pinMode(ledPin, OUTPUT);
    pinMode(buttonPin, INPUT); //This is used to read into pin 3
  }

void loop(){
    while(led == true){   //A while loop is used to repeat a section
    //code as long as the condition in the brackets is met 
    int val = digitalRead(buttonPin); //val is used to find out the
    //value coming from the tactile switch
      digitalWrite(ledPin, HIGH);//Used to light up the LED 
      if(val == LOW){
         led = false;
         delay(500);
      }
    }
   while(led == false){   
       int val = digitalRead(buttonPin); 
      digitalWrite(ledPin, LOW);
      if(val == LOW){
         led = true;
         delay(500);
      }
    }
  }
									

This week we used “if” and “while” statements, which are fundamental in programming and are used to execute commands in a certain order and under certain condition. A “while” loop is used to repeat a segment of code till a certain condition is met eg. while(int x < int y) or while meeting a certain criteria eg. while( boolean x  == true).

An “if statement” is used to execute code only when a certain criteria is or isn’t met eg. if(x == 1). “If statements” can be used with an “else” clause to execute code that when the if-condition isn’t met eg. if(x == 1) {runCode();} else{runOther()}

** IMPORTANT**

To use “if” and “while” statements, you need to understand Boolean operators. Boolean Operators are used to compare two values. Boolean operators return true or false.

List of important Boolean operators:

  • == Equal
  • != Not equal
  • < Less than
  • <= Less than or equal
  • > Greater than
  • >= Greater than or equal

To add to these we can use && and || to execute parameters along side each other.

&& : AND (eg. if(x == 1 && y ! = x) ) in this scenario the code will only execute if both are true

|| : OR (eg. if(x ==1 || y != x)) in this scenario the code will execute if one or both are true

Arduino is as much about programming as it is about hardware. Using what you learnt this week you can go and play with code and buttons with LED’s. Try adding extra buttons and LED’s and see if you cant make something cool and remember to share anything awesome with us in the comments. If you want the Fritzing files or any other resources, ask below in the comments section.

See you guys next week, where we’ll learn how to fade a LED.

Avatar of Dan

by Dan

Tutorial one: Light it up!

October 15, 2012 in Arduino, tech fruits, Tutorials

Hey its Dan, the Tech-Fruits intern, and this is my first segment in what I hope to turn into a weekly post about the Arduino. This week I’m going to do an intro to Arduino programming and prototyping. In this post we’re going over the simple “Blink” sketch, which is widely regarded as the “Hello world” of electronics and physical computing.

You’ll need the following:

  • 1x Arduino (Uno)
  • 1x LED (Red in this example)
  • 1X 560Ohm Resistor
  • 2x Jumper Wire

First of all, we need to set up the circuit and to help you with that I’ve included an image to layout it out. For those of you looking to make your own images and schematics, I made this one in Fritzing, a beginner friendly program for planning prototypes and designing your own PCB’s. I can also send you the .fbz file to manipulate this sketch and add to it. Just comment on the article, letting me know if interested.

As the next step, using the Arduino IDE  (for the operating system of your choice) you code your sketch. The sketch for this lesson is included in the example section of the Arduino IDE, however I edited the comments to help understand what is going on and to give some basic programming pointers along with the electronics. Again if interested, I can send you a copy of the .ino file to upload to your Arduino. Below is the sample blink code used on the Arduino. 

//First of all I think it is important to explain what a comment is.
//A comment is basically used by a programmer to explain what his code is doing.
//To comment you need to use // for a single line
/* or /*

int led = 13; //used to give the variable "led" the value 13 (set to pin 13 on the Arduino)
//int stands for Integer
//the setup routine runs once when you press reset:</code>

void setup() { // function used to tell the Arduino to wake up a pin for use
pinMode(led, OUTPUT); // initialize the digital pin as an output.
}

void loop() { //the loop routine runs over and over again
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
 
									

Using the above code, you created your first working Arduino circuit. Good job, you’ve cut you’re teeth in the world of electronics and physical computing and taken you’re first steps towards becoming a hacker! :) Till next week enjoy and be sure to share with us any projects on the forums or on Twitter.