Tuesday, November 29, 2011

Arduino going mainstream

I saw an announcement that Sparkfun has put together an Arduino Invetors kit and it's available to order on Amazon US (SparkFun Inventor's Kit for Arduino)

It's great to see the Arduino going proper mainstream.  Up until now I've bought my Arduino bits from specialist electronics hobby sites or ebay.  It looks like tinkering is becoming more popular.

Saturday, November 12, 2011

Arduino - Light Dependent Resistor used to control brightness of LED

A light dependent Resistor (LDR) changes it's resistance based on how much light hitting it.

I know this can be done without the Arduino, I am dong this to get the bits together for a more complex project.
Making sure the individual pieces work before putting them together.

I read the LDR on analog pin 1 and then used PWM on pin 6 to set the LED.

// Background Light (the brightest it will be)
int LDRMax;

// When LDR is value returned (depends on the resistor in line with the LDR (I used a 1.5K)
int LDRMin = 50;

// Ratio of range to 255 (max analog out reading)
float LDRRatio;

// Analog Pin for reading LDR
int LDRPin = 1;

// The Value read for the LDR
int LDRValue;

// PWM Pin for the LED
int ledPin = 6;

void setup() {               
  // Initalize LED Pin
  pinMode(ledPin, OUTPUT);    

  // Enable Serial Communication - useful for debugging
  // Get the maximum LDR Reading
  LDRMax = analogRead(LDRPin);

  // Print to serial
  Serial.print("LDRMax: ");

  // Calculate the ratio (note the use of float - since the calculation is being done on integers you have to state this is a float calculation
  LDRRatio = (float)255/(LDRMax-LDRMin);

  // Print to serial
  Serial.print("LDRRatio: ");


void loop() {
  // Read the LDR
  LDRValue = analogRead(LDRPin) - LDRMin;

  // Print the value to the monitor so I can see it

  // Modify reading to match analogWrite range using Ration calculation
  LDRValue = (LDRValue
  ) * LDRRatio;
  // Sometimes the number is over 255 or under 0 - rounding errors !!!!!
  if (LDRValue < 0){LDRValue = 0;}
  if (LDRValue > 255){LDRValue = 255;}
  // Print to Monitor 
  Serial.print("LDRValue after calculation: ");

  // Set the value for the LED
  analogWrite(ledPin, LDRValue);

Still figuring out the Arduino coding and also auto calibrate the LDR so I have the full range.
In the video the range of readings was from 30 to 155.  The scale is 0 to 1023 so I'm only getting a small portion of the range which means I have to get this right to go from full brightness to off.

Tuesday, November 8, 2011

Arduino - Continuous Rotation Servo

As part of my usual trawl around the Internet to learn a bit about electronics I came across the following page on Maker Wiki page.


It explains how to modify a servo so that it can rotate continuously clockwise and anti-clockwise.  Since I bought a pack of 4 servos on ebay I decided I would see if I can get this to work with my really cheap servos.

I took apart the servo and found it was a bit different.  Firstly, it had screws rather than being held together with plastic clips.  A bit of a result for me as it meant I could easily take it apart without fear of breaking the housing.

The base metal stopper was the same but the plastic stopper was on one of the cogs rather than being on the casing.  Again, fairly easy to take into account.

I then had to disconnect the little PCB and wire in the 2 resistors.  This is where I found out I'm a bit ham fisted and out of practice on the soldering.  I eventually got it done and re-assembled the servo.  It's a really tight fit in there but the screws made it easier to 'squeeze' it all back in.
I didn't have 2 x 2.2k Ohm resistors so I used 2. 1.5k Ohm resistors.  No idea what difference it makes but it worked.  If somebody can explain the affect I would really appreciate it.

Then it was down to attaching it to the Arduino.

I chose Pin10 for no other reason than I used it before to control a Servo.

As per the Maker guide I had to find the centre value.  For this servo it was 77.  If you do this your will probably be different..

I attached the servo.
Brown - Ground
Red - 5V
Orange - Analog in

It started rotating so I knew the mechanical bit worked.

The code below for the Arduino is quite simple as it was just for a test. 

// Servo Continuous
// Code to spin a servo modified to do continuous rotation
// Small cheap 9g servo modified using 2 x 1.5k Resistors
// and details from: http://makeprojects.com/Wiki/Servos

#include <Servo.h>

int CStop = 77;  // The value where the Servo is Stopped

Servo myservo10;  // create servo object to control a servo on pin10

void setup()
myservo10.attach(10);  // attaches the servo on pin 10 to the servo object
pinMode(13, OUTPUT);   // Using the LED on the Arduino UNO to know what is going on - not reallt needed 

void loop()
    // STOP - Setting to CStop stops the Servo
  myservo10.write(CStop); // Put servo stop
  digitalWrite(13, HIGH);   // set the LED on

  // Pause

  myservo10.write(5); // Rotate servo CLOCKWISE
  digitalWrite(13, LOW);   // set the LED on
  // Pause

    // STOP - Setting to CStop stops the Servo
  myservo10.write(CStop); // Put servo stop
  digitalWrite(13, HIGH);   // set the LED on

  myservo10.write(150); // Rotate servo ANTI-CLOCKWISE
  digitalWrite(13, HIGH);   // set the LED on
  // Pause

Video showing it rotating - yeah...

 My real aim is to use at least 2 of these to drive a vehicle of some sort.  Maybe use the PS2 controller to control it.  Or try to create a line following robot (not really a robot but that's appears to be what people call them)

I promise next time I will take pictures of the process for modifying the servo so that you can see what it looks like.

As a guide to do this first one it took me over an hour to get it done.  As I said a bit rusty...