Friday, November 21, 2014

Continous rotation servos

Back after a small hiatus with a somewhat short post.  So today we talk about continuous rotation servos. What are they exactly?
Well continuous rotation servos look exactly like normal servos. Figure 1 shows you one continuous rotation servo.



Figure 1: Never judge a servo by its looks

Ok, nothing special here. Let's draw the line between a servo and a continuous rotation servo.

SERVO - Feed it some value and it will move to some position/angle. Speed is preset.
CONTINUOUS ROTATION SERVO - Feed it some value and it will rotate at some speed. You can vary both speed of rotation and direction of rotation. Is to servos what the evo is to mitsubishis. A completely different monster.

Right. You can vary both its speed and direction of rotation. That is particularly useful if you are building a differential drive robot. A differential drive robot is based on two motors that are independent of each other. The video below gives you a demo of one differential drive project I have been working on, during calibration phase. 





Calibration is extremely important here. I need to know the pulses that must be fed to each motor so as to obtain a specific motion. After a long phase of testing, you'll have gathered all that data, thereby getting what I call the engine map.
Let's get things clear here. Yes, I can vary the speed and direction of rotation of the continuous rotation servo and the manufacturer will often provide timing pulses for that.
If you are on arduino, commands(manufacturer) would be like:

myservo.writeMicroseconds(1500) =>Full stop
myservo.writeMicroseconds(2000) =>Full speed right
myservo.writeMicroseconds(1000) =>Full speed left

Now, the world would be beautiful if everything followed such brilliant precision. However, it's not. You are gonna have to obtain more accurate values for these data. If you are on a differential drive, double the precision. For making turns, you need for two wheels to be perfectly synced. Now, as you saw, it's not the case. Working with those perpendicular lines was a way of calibrating turns. After a really long and incredibly frustrating testing period, I came up with the engine map. 
Usually, if you vary between the extremes, you can vary the speed. The video below demos the capacity of a continuous rotation servo. I'm just crudely mapping the readings from my potentiometer to my servo.



Now, why do I think they are a good bet for differential drives? After all, dc motors coupled to H bridges and encoders would do the trick. Here's your answer. It's plug and play. No need to cram your design with additional components(like H bridges and encoders).
That's it. Thanks for reading. More next week.




Friday, November 7, 2014

Controlling your Arduino from your phone via bluetooth

Controlling your Arduino from your phone via bluetooth is simple business. I would  have liked having been told that a few years ago. It's just that to get it done, you need resources that are spread throughout the web. In this post, we'll build a simple controller. Our aim is to switch an LED on/off . All that, wirelessly.
So, where do we start? We could ramble on and on about the various theories on Bluetooth but chances are you need a quick fix solution. That is what we'll be doing here.
Ok, so parts we'll be needing:
1. An arduino
Well, any arduino would do but I'm using an arduino uno
2. An LED
Classic 5mm LED
3. A bluetooth module
The bluetooth module is essentially the component which takes care of the transmission and reception of the data. I'm using a bluetooth mate silver which is shown in Figure 1.



Figure 1: The BT mate silver



4. A phone with some serial communication software
I'm using a Lumia 625 and despite the limited limited LIMITED(did I say LIMITED? It's that bad) number of apps available on their store, I grabbed a small serial communication software called BT Terminal, shown in Figure 2. Many many thanks to the creator of this app.


Figure 2: The App
5. An RGB
An RGB is essentially 3 LEDs in one package. They are insanely cool.


Figure 3: The RGB

6. Some wires
First things first. You need to power your project. Be it the 5V regulator, usb power, anything that works will do. So in my case, I'm just plugging the UNO to my computer's usb port.
Then, you need to connect the components together. So connections are

A) Bluetooth mate silver module
Now this is advanced stuff. So, the module will communicate at some speed and we need to know that speed. I think the BT mate silver's default speed is 115000 bauds. You can change that to values that suit you by configuring the module. If you don't want to do that, you'll be using 115000 in the program. I've set mine at 9600. The module comes without header pins, so if yours is out of the box, you have some soldering job on the line(be careful to not leave your iron on these pins for too long)
There are some marking on the board, so:
1. VCC to 5V power
2. GND to Ground
3. TX-0 to Arduino Pin 2
4. RX-1 to Arduino Pin 3

B) LED
Simple job
1. Ground it
2. Connect the +ve lead to pin 5 via a resistor(anything between 1k and 2k is OK)

C) RGB
The RGB is cool once you know it. So longest lead is grounded and the remaining three pins are connected to Arduino Pins 6, 7 and 8 via resistors. You should have something like this.



Figure 4: Setup


Now, for the code:
/* 
Simple Bluetooth controller project
Sordagen Bryan Arnasala
7 November 2014
*/
#include <SoftwareSerial.h>
//Setting up trans and recept pins
int BTtx = 2; //Arduino Pin 2 is connected to TX-0
int BTrx = 3; //Arduino Pin 3 is connected to RX-1
SoftwareSerial BT(BTtx, BTrx); // Serial comm config, I'm using BT,anything'll do
//Setting up led and RGB pins
int LED = 5;
int RGB1 = 6;
int RGB2 = 7;
int RGB3 = 8;
char TOKEN; 

void setup()
{
  BT.begin(9600); // Careful here. Make sure you have the proper value
  pinMode(LED, OUTPUT);
  pinMode(RGB1, OUTPUT);
  pinMode(RGB2, OUTPUT);
  pinMode(RGB3, OUTPUT);
}

void loop()
{
  while (!BT.available()); 
  TOKEN = BT.read();
  if( TOKEN == '1' )
  {
    digitalWrite(LED, HIGH);
    digitalWrite(RGB1, LOW);
    digitalWrite(RGB2, LOW);
    digitalWrite(RGB3, LOW);
  }
  else if( TOKEN == '2' )
  {
    digitalWrite(LED, LOW);
    digitalWrite(RGB1, HIGH);
    digitalWrite(RGB2, LOW);
    digitalWrite(RGB3, LOW);
  }
  else if( TOKEN == '3' )
  {
    digitalWrite(LED, LOW);
    digitalWrite(RGB1, LOW);
    digitalWrite(RGB2, HIGH);
    digitalWrite(RGB3, LOW);
  }
  else if( TOKEN =='4')
  {
    digitalWrite(LED, LOW);
    digitalWrite(RGB1, LOW);
    digitalWrite(RGB2, LOW);
    digitalWrite(RGB3, HIGH);
  }
  else if( TOKEN =='5') //Turn off all LEDs
  {
    digitalWrite(LED, LOW);
    digitalWrite(RGB1, LOW);
    digitalWrite(RGB2, LOW);
    digitalWrite(RGB3, LOW);
  }
}

Ok, now just upload this. Load your serial comm app. You might have to do some straight forward bluetooth pairing and you are off. Commands "1" to "4" switch one colour on. Command "5" turns off all of them. Here's the project at work.




That's it! If you can switch LEDs on and off, you could do the same for motors, sirens, fans...anything! You might want to read data from sensors. This is easy to do. The BT.println() command does that. You might also be cognizant in App development - you could then build an app that is custom for your project. 
Thanks for reading!


Friday, October 31, 2014

Getting your PCB done from A to Z

PCB or printed circuit board is a major step up from the veroboard for DIY hackers. I learnt about PCBs or PCB printing during my final year project as I realised that dangling wires, ugly solders points and loose parts would be a pain. So I produced a lot of sub par PCBs before finally getting to really good ones. In this post, I want to have you skip this steep learning curve and jump right to a good work. That explains why this week's post is so long.
Okay, let's first compare veros and PCBs. Figure 2 shows a veroboard which housed a microcontroller, a 5V regulator and a few pin outs. Figure 1 is exactly the same circuit, but on a PCB. The gap in terms of quality and neatness is blatant.


                                                      Figure 1: PCB version Print Layer


Figure 2: Vero version 

Okay, so in this post I want to have you build your own PCB. Now, I've been thinking hard on what circuit to work with. I looked at the 555 flasher, 555 timer and I realised it would be great to work with a much more useful circuit(no disrespect to the living legend that is the 555). So, my very first post was about regulating a 3s lipo(about 12V) to 5V without having to bother about heat sinking issues. Well, this is the circuit that we'll be laying down on PCB. I think it is a extremely useful circuit that will come in handy. If you want a neat design to shift a 3s Lipo or even Car battery voltage to 5V,we've got a deal.
So where to start in any PCB project, make sure you get your circuit diagram right. Figure 3 gives the circuit diagram for our regulator.

Figure 3: Circuit diagram

Now, I've tested this circuit and it works. Everytime you come up with your circuit diagram, make sure you just test it on the good old breadboard(fig 3a). I know its a pain and you are probably in a hurry but it's important that the circuit works before going to PCB printing. 


Figure 3a: Bingo 1 - The LED is on

Here's a video demo of the circuit at work:

      

Bingo 2 - We have 5V


Okay. For this circuit, we'll be need the following components:

1. MC34063 - Switching regulator
Now I've not seen this in mauritius but I've got quite a pile of them lying around. Just get in touch and I'll get you one.


Figure 3: The switching regulator

2. A 180uH inductor. Actaully, you could even use a 200uH. I've done the maths and it does not differ much. Now, be careful with this one. At a local electronics store, I ended up with a capacitance! So here how it should look like.



Figure 4: The inductor

3. A 0.33 ohm resistor. Figure 5 shows how it looks like. Well, there are variants ;)


Figure 5: The 0.33 ohm resistor

4. Capacitors.
   - One non polarized 470pF cap
   - One polarized 47uF cap
   - One polarized 100uF cap

5. Resistors(1/2W to be safe)
  - One 1kohm resistor
  - One 3kohm resistor 

6. One fast switching schottky diode 1N5819. Now, here I got the 1N5822. It's huge! But it does the job :)

7. Two 2 port screw terminal, shown in Figure 6.


Figure 6: 2 port screw terminal

8. 8 pin IC socket

Okay, now that we've got the parts, its time for PCB design. So it's time to transpose our circuit to PCB. How do we do that? A software. Now, I've been using Pad2Pad. It's a fantastic little CAD program that offers a powerful interface and great simplicity. You'll pick it up in no time!
(Disclaimer: Pad2pad belongs to its respective owner. Snapshots used are not meant as copyright infringement and are used only for educational purposes)
So, once you have the software installed, time to move to design. Now, open up pad2pad.
Figure 7 shows the startscreen. Just click 'OK'.



Figure 7: Pad2Pad startscreen

Next, you'll be ending up with a black screen with grids as shown in figure 8.



Figure 8: Screen 2

Now, for this project(and most porjects actually!), we'll be using only two tools. The pad tool and the trace tool. They are shown in figure 9. The rectangle profile(blue) represents the limits of our PCB.  Don't worry about that for now. We'll tweak it later.


Figure 9: The tools

Before moving to design, we need to tweak the thickness of the pads and the thickness of the lines. The pads are where you'll be drilling holes for your components. The lines are actually connections. So, set the pads diameter to 1.91mm. Click on the pad tab. Then move to the top right. You'll find something like 1.00mm. Scroll down and select 1.91mm. 


Figure 10: Setting Pad diameter

Similarly your are going to change trace width to 0.64mm. This is shown in Figure 11.


Figure 11: Setting trace width

Okay, simple stuff first. Try to replicate figure 12. You are going to breeze through this. Simple drag drop stuff.


Figure 12: Getting started

Pretty easy! Next up, our voltage regulation circuit. It's given in figure 13. You'll notice the holes are different.We are using the padded hole option. Why? Drilling would be less tedious.


Figure 13: The final layout

Now, this may look daunting, but you'll get it right! Okay, once you've done this, you have to print it. ALWAYS make sure to select MIRROR when you print(Fig 14). Otherwise, you'd be getting a circuit that is reflected.

Figure 14: Printing it right

Finally, you'll be ending up with something like this.


Figure 15: Print out

As you see, its inverted or reflected. Now, the next thing to do is to get the PCB printed. Now, I'm scared of chemicals. I was horrible at chemistry experiments. So, I stay away from those stuff. What you can do if you have the same issues is to take your printed sheet to local electronics store that do PCB printing.
When you get there, there is only one thing to say - "This is the solder side". That will do the trick. It will probably take a day or two. Around Rs 75 is what I paid. From what I observed, it's based on the size on the PCB.
Now, you'll notice I did not inscribe the components on the PCB. I didn't because it does not look good on the finished PCB.
So, fast forward a few days later and you have got your PCB. Figure 16 shows mine. The borders may not look perfect but please be careful if you attempt to cut it. The dust is hazardous.


Figure 16: The PCB

Time for some soldering! Now, there is a very old feud between me and soldering irons. I've had 4 or 5 of the cheap ones and they kept overheating. Finally, I found the one. Never failed me. Behold the Stanley(Figure 17). If you have no background in soldering, I'm planning to have a complete soldering tutorial in 2 weeks(Friday 14 November).



Figure 17: "The" Stanley
Okay, when you look at that PCB, it's pretty difficult to know what goes where since there are no labels. Here are the labels. 


Figure 18: Labels

This is how the PCB would look it were transparent.
Finally, your populated PCB should look like that. 


Figure 19: Populated PCB

I replaced some of the resistors which were not available. You may have to enlarge some of the drilled holes. That's it.
Next week, how to control your arduino from your phone via bluetooth!

Friday, October 24, 2014

On temperature sensors

Temperature sensors! We've all at some point played with one of those. Be it that water temperature sensing application or the thermostat in a car. There are several types of sensors and going through all those data sheets would reveal that there are disparities between them. In this post, I'll try to present some sensors based on stuff I've seen.

1. The Analog sensor or thermostat or thermistor
Okay, this is the one. I'm pretty sure all DIY hackers have seen this one. A thermistor is basically a resistor whose resistance varies with temperature. Just like the LDR's resistance varies with light incident on it. So what does it look like?
Figure 1 shows basic thermistors. Those two are identical except for their temperature ratings. When you ask for a thermistor at the electronic shop, this is what you'll most likely end up with.


Figure 1: The basic thermistor

Sometimes, you'll be working on a project where you'll be measuring the temperature of a liquid(maybe water). The water proof thermistor is what you might be using. It is shown in figure 2.


Figure 2: Waterproof thermistor(right)

So is it any good? I think that if 
1) You need a simple temp sensing solution. This is for you.
2) You have tons of analog ports on your microcontroller. This is for you.
3) You are not bothered with a great deal of accuracy. This is for you.
4) You are looking for a very cheap solution(Less than Rs 50 in mauritius). This is for you.

So, simplicity and low cost are the key words here. So how to make that work? This is a variable resistor and so to get something coherent out of it, we fall back to our good old voltage divider circuit. Figure 3 gives a brief recap.



Figure 3: Voltage divider recap(Quality is top class, I know :p)

So, A gives a basic voltage divider circuit. To keep it simple, if you have a closed circuit with a DC source and two resistors, the voltage is going to be divided at some points. For instance, at Vo, the voltage is given by equation B. Now, suppose you take R1 and replace it with a variable resistance(for instance, the thermistor) - if temperature changes, Vo will change. That's it for the maths.
C gives how you should set the circuit up if you are using a microcontroller. Usually, if your thermistor is rated 10k, R should be 10k. And then it's quite easy to convert the analog data to some coherent temperature value. If you really dig up stuff, you are gonna be facing terms like NTC, PTC. But if you don't really want to bother yourself with the maths, here you go. If you're using the UNO(or Pro or Mega), 1024 is the highest analog reading you are ever going to read. So, lets call the analog reading you are getting TEMPRAW. Also, lets say Rt is thermistor resistance and R is the value of the fixed resistor. Figure 4 gives the relevant equations.


Figure 5: Thermistor equations

So that's it, a very simple means of getting temperature data. Next up, the analog temperature sensor.

2. TMP36 -Analog temperature sensor
Okay, I have a bit of a personal history with the TMP 36. I never managed to get a working one - I think it's just bad luck :/ . So, how does it look like? It's very similar to a transistor and it's extremely easy to use. And if you are looking for a quick upgrade in terms of accuracy over the thermistor, you have a deal. This beautiful piece of electronics offers an accuracy of 0.1 degrees celcius. Figure 6 gives you info on pinouts and how to set it up. 


Figure 6: Art ain't in me

Now that you have Vo fed to your analog pin, the conversion step is much easier than for a thermistor. The TMP36 has a sensitivity of 20mV/degrees C. This implies that for every degree celcius change, you are getting 20mV. Now, you'll be reading stuff from 0 to 1023. To get voltage from this value, you'll have take this value, multiply it by 5000(assuming you are using 5V) and divide it by 1024. To get a degrees C reading, take the voltage, substract 500 and divide by 10.
Next up, a personal favorite, the DS18B20.

3. Digital temperature sensor, DS18B20

I came across the DS18B20 because I had run out of analog pins for a project. It is a wonderful sensor in terms of functionality.It saves you the precious analog pins and that makes it stand out. The downside of this sensor is the sheer complexity of getting a reading out of it. So complex it is that people have produced libraries for it. So if you want this sensor, the onewire.h library is essential.
Another issue you might be facing is conversion time. The resolution of this thing is adjustable from 9 bit to 12 bits. At 12 bits, conversion time is 750ms. 750ms! In real time applications, you are going to get in trouble with this. I set it up for 9 bits and this reduces conversion time to just 93.7ms. A more acceptable value. So I did not find software to do that. I tweaked the existing code a little bit and it just worked. You might want to dig in the datasheet for that one.

Next week - PCB printing in Mauritius!


Sunday, July 6, 2014

Up and running with a PIC16

Up and running with a PIC16
This tutorial covers basics on how to hook up your PIC16F877A, write a small program and upload a program to your chip.
Section A : Wiring up the chip
Stuff you will need: A PIC16F877A, A breadboard, PICKIT 3, A 10k resistor, a 20Mhz xtal and an LED.
           

Crystal

Wiring this chip is very simple. You just need to follow the following instructions.
1.       Place your chip on a breadboard. Remember how to read the pins(small notch indicates pin 1)
2.       5V to Pin 1
3.       5v  to Pin 11
4.       Ground to Pin 12
5.       Crystal pins in Pins 13 and 14(Polarity does not matter)
6.       5v to Pin 32
7.       Ground to Pin 31
After successfully following these steps, your breadboard should look like this.


On breadboard

Section 2: Writing a small program
In this section, we take a look at how to write a led blinking program.
The program is first written in MikroBasic©, in BASIC LANGUAGE.
To proceed, follow these instructions:
1.       Get a free demo copy of Mikrobasic(http://www.mikroe.com/mikrobasic/pic/)
2.       Open Mikrobasic and click on new project.


Screen shot 1

1.       Click next on the new project wizard window pop up.
2.       Select PIC16F877A and Set clock to 20Mhz.


Screen shot 2

1.       Click on Next and Finish
2.       We will now write the program after taking a look at the fundamentals of BASIC Programming.
Format:
Program YourProgramName
Main:
     Code
End.

The following program will flash a led on port B.
program cassosl_1
main:
 TRISB = 0                            >Set Port B as output
 while true                          >Start an infinite while loop
  PORTB = %11111111      >Set all outputs of PortB to 1(HIGH)
  delay_ms(1000)               >Set a delay of 1000ms
  PORTB = 000000      >Set all outputs of PortB to 1(HIGH)
  delay_ms(1000)               >Set a delay of 1000ms
 wend                                   >End loop
 end.                                     >End Program

3.       Click Build. Many files will be generated in your save folder. You should look for an HEX file, in particular. Copy and paste it on your desktop.

Uploading your program:

In this step, we take a look at how to upload your program to your chip. In this step, you will need a programmer. The programmer is an interface between the chip and your computer. It usually has 6 output pins.
I use a PICKIT 3.

The PICKIT 3


Pin outs

You will connect lines from these output pins to your chip as follows:
PIN 1(ARROW) – Pin 1
PIN2 – Power
PIN3 – Ground
PIN4- Pin 4O
PIN5-Pin 39
PIN6-No connection
You should be having something like this.




Final layout

Next,  you should get MPLAB IDE and load it.
Click on Programmer?Pickit3.
You will get an error message. Click Programmer, settings, power. Set to 5V.
Next, click file, import and select the HEX file. Once loaded, click program.



Snap shot 3

Finally, connect a led to any pin out of port B and it should flash.







Saturday, July 5, 2014

The fantastic switching regulator

Time and again, we need to power a system from a battery rather than from mains. Now, the issue here is that most microcontrollers should be hooked up on 5V(or 3.3V). A battery would typically provide voltages above 10V. What you have below is a lithium polymer(LIPO) battery rated at 11.1V.

                                                          The LiPo -  a dangerous beauty

So, in that case, we would need to "step down"(to convert a higher voltage to lower voltage) that voltage. Now, there are two main solutions for that - either
1) A linear voltage regulator or
2) A switching regulator
Let's have a look at the first option, namely, the linear voltage regulator. Most of us know about this thing-it will get the job done all the time. So, what you have below is basically a functional 12V to 5V step down circuit using the linear regulator.

The linear regulator at work

Now, this is a nice circuit if you are doing simple things(flashing a few leds, reading a few sensors). However, things literally heat up when you increase the load. So, for instance if you are running a few motors, you may may observe the following symptoms - frequent restart or total shut down(your circuit appears dead). Why is that? You see, the linear regulator is device that dissipates heat and as your circuit demands more current, more heat is generated. Normally, your heat sink(fin like structure in the figure) would take care of that but sometimes even that may not be enough. If you stick with that, you may have to for impractical stuff like fans or whatever.
To understand the flaws that inhabit the linear regulator, we have dismantle the black box abstraction - we have to look at its principle of operation. What we have below is a simple schematic of the linear regulator.

Linear regulator schematic

In very simple terms, the output voltage is compared to a certain reference voltage and based on the result of this comparison, the resistance of the series-pass element(basically a transistor which you can use to regulate the current passing through a line) is varied from the two extreme modes of operation. Let's open a parenthesis here.
What you have below is a greatly abstracted characteristic curve of the transistor(we are not bothered about the details).

Simplified Characteristic curve(pardon this Blasphemy, God of electronics)

So, in this figure, you can actually visualise these two extreme modes of operation. Parenthesis closes here.
Ok, coming back to those two modes, we've said that the transistor is going to operate in the spectrum between those two modes. That is where the problem lies. In saturation mode, voltage drop across the element is low and power dissipation is low as well. In cutoff mode, the current through the element is low and the power dissipation is low. However, in between those two modes, we are dealing with a different animal. That is where the heat and the need for a ridiculously sized heat sink come from. In the figure below, you have a circuit for a heat pad I played with sometimes back. Notice the thin plate - THAT'S the heat sink.

   Five minutes after power on and you have a nice BBQ grill

With those elements in mind, we could argue that a better solution would operate either in cutoff or saturation. Turns out we have one which operates alternatively in both. Enter Switching regulator.
The first time I dealt with the switching regulator, I was really in a bad shape. I just needed something that would regulate my battery voltage down to 5V and give me currents around 1A. And it did just that. What is it all about? 


The MC34063 switching regulator

As you can see, it's just an 8 pin IC. Of course, you need some passive components(resistors, capacitors and  inductors) to get a functional circuit. There is a science behind the values of these components but there is a lot of maths involved(I discuss this in another post). However, if you need to step down 12V to 5V with currents of 1A, I've got that covered.
So, here's the schematic(made with Eagle):


Schematic
In the picture below, you have the circuit mounted on breadboard.
It's important that you use a fast schottky diode(1N5819) instead of the traditional diode. I had some trouble finding the 1N5819 locally but the 1N5822 should do fine.