Laser Tank - Basic tracked robot with Targeting Laser pod!







CURRENT IN AND OUT (meaning that you can track both battery life circles and charging) TEMPERATURE





It's been a while I was using only my Basic Stamps for building robots... But Basic Stamps lack both the speed and the flexibility for most applications even though the are perfect for learning structured programming and the BASIC language. So now the time has come for me to use AVR in my robots... AVR are nice microcontrollers with many features, like interrupts, timers and so on..., they are much faster that the Basic Stamps and the have bigger memory allowing us to write more code... Also they are very easy to use requiring only power to operate (and if you like a crystal - osclinator) and very easy to program (There are many nice circuits out there in Google... I just prefer the 10pin STK500 ISP). So in this project we'll be using the ATMEGA16 @16MHz. But as every project... it must have something special, something to be able to look different from other... But what???

I 've seen many robots with photosensors (photovore, photophobe), many linefollowers, many object avoiders, some heat seakers, sumobots, micromouse and others... BUT, very few had a battery monitor... Most of them run on a new or freshly recharged battery... If the battery was drained and wasn't simply enough capasity to feed the robot???

That's why I incorporate a Dallas Semi DS2438 in my circuit board... I don't like usually to advertise but this chip had everything... A current sensor, a voltage sensor, a thermometer and so other goodies we won't be needing...


Now navigation... Since it'll be a simple navigator - free roaming robot (with some extra features) I'll be using a simple IR_pair for the robot, but if I have time (aaa exams exams exams exams they are killing me!!!) I'll also include a Sonar... The pair will consist of a TSOP1907 and some single IR Leds...


And now it's picture time... Since I have assebled the case years ago and don't like to rip it up right now... I'll be showing some basic pictures of the case of the robot explaining how it was built and which are the thoughts for the future =P....

So here is an overview of the robot case:

Main Case Overview


And here is the front view of the robot... As you can see the case is quite close to the ground but our subject here is not machining clearly but programming the robot behaviors...

Front view


Here is the bottom view of the robot... It used to be used as a place for the batteries but as experience grow it teaches me to place the battery (and any other heavy thing) in the front of the robot to be able to climb hills with easy... But more details coming...

And the bottom of the robot


And here is the machining tips for the wheels!!! I got some servo cross-like horns cutted 'em to fit the inner of the wheel, placed a screw in an empty hole of the wheel and glue it for safety with some super glue!!! And finish... Also I kept the outter axis of the wheel (it was there from the time I bought this poor toy) and after shorting it a little, I fitted it back in. So the Case and the wheels are bond together making the locomotion system more stample than ever. NOTE: that the locomotion system that was with the toy was extremely weak that's why I placed in the servos!!!

Finally, and also very important, after mounting the servos to the case I jointed 'em with that golden pipes (don't know how to call 'em sorry). And here we are... the locomotion system and the case in finished!!! Now it's batteries' time...

And some of the wheels machingAnd the servos joints


And now the hardest part for me... mounting the batteries... Batteries usually are the heaviest part of a robot. Because we have a tracked robot, placing the batteries in the front is crusial for the balancing of our robot and it's ability to climb step hills!


By placing the batteries that way:

Battery holder


I hane the robot balancing it's weight at about the middle of the robot so it's just very ok!!! As you can see the holder is very simple just a part of a pcb mounted to the case with some screws and glue and so long pin headers to hold the battery. So with the battery, it should look like that:


battery mounted


And now the big deal... The electronics. I have been trying to use two microcontroller since the beginning but always something was about the fail... LCD fail, IR fail, frequency generator fail... And do on which I really hate to describe (my hours being lost for nothing). And the biggest of my problems... That f*ckin# TSOP were broken, they were old - yes, but broken??? AAggghh!!! All my Sunday for a pretty nothing... Uugghh, sorry for this but I think everyone can understand me... So, finally, I had 'em replaced with some nice Panasonic ones I had... (desoldered 'em from older boards I have made and almost never used...) Surpassing these promblems after many hours having been wasted I finished the main board consisting of a ATtiny2313 @ 20MHz that I won't refer to cause it's never used, the main processor which is an ATMEGA16 @ 16MHz, a simple regulation circuitry, a NE555 tuned at ~38KHz for the IR leds and the pride of the board! The DS2438!

Before I get any closer to the software I'll give the schematics...


So here are the schematics for the main board of the robot:



The regulation circuit consist of a simple 7805, some filter capacitors and some anti-current spike capacitors

And here it is:


And now the NE555 schematics:


So after all these the board should look like that:



And finally the board mounted on the case!!! I feel just nice...




This comes after a time I know... BUT... Many things dramatically changed on LASER Robot Tank so,

I got to do things almost from the beginning.





Yes, the remake guys... It was about time... See below...

Laser Tank was an old idea… Actually the pictures above shows version 2, which didn’t satisfy me at all. Although the basic functions did work, they didn’t work well…

Problems occurred… Many… The IR weren’t working well and the frequency they emitted wasn’t controlled by the microcontroller. Also, very very lite sensor background…

By controlling the frequency of the IR you can determine the detection range of your sensors…

In version 3 the IR LEDs are connected directly to the microcontroller. And not controlled by the NE555. For this we employ the 16bit timer of our ATMEGA8535, timer1.

We set the compare value so that our timer produces a 38 kHz to 44 kHz signal. NOTE that the higher the frequency the lower the detection range. This is because the higher the frequency is the smaller the wave length is. Shorter wave length means shorter travelling distance because the mean of travelling (which in our place is atmospheric air) remains the same.

The frequency is delivered on our OC1A pin which in ATMEGA8535 is PortD.5 which is connected to a system of transistor, playing the role of an amplifier with a variable gain.

This is for controlling the current through LEDs so we can further adjust their brightness and further more the detecting range.


But remind me why we wanted that frequency after all…

As object detectors we use two IR pairs. The emitter is a common LED… Not much to say really… The receiver is a TSOP1738… As it suggests 17”38” it can recognize a 38kHz carrier transmit. But this is for an ideal world where no wars occur etc etc etc…. In the real world the 38kHz filter is not perfect and let’s other frequencies pass… So the sensor will recognize a carrier from 36kHz to 44kHz maximum…


Now that we can control the distance that our sensors detect objects, we can basically do miracles… One great thing we can do is not only follow an object but also keep a steady distance between the object and the vehicle… Although I talk with such a pride about it I had some problems with the code… Although I had a steady distance between the robot and the object the follow the object code didn’t work so well…

First, we will explain how the whole thing works. In front of us (robot) there is a field, right? Right. We can separate this field of view, in smaller areas. Imagine the closest area just in front of the robot. Consider that objects in this area can always be detected by the sensors. A little further there is an area that objects are detected, but not always. Even further there is an area that sensors can hardly detect an object. Are we straight that far? Nice. But if the frequency of the IR pair is fixed we just see if there is an object in a certain area in front of the robot, right? Right! Nice… We can use this in our advantage. Imagine now the far area that sensors hardly peak up. If we set the frequency close to 36 kHz, we have a wide detection range, right? Right! But if we set the frequency close to 44 kHz, we won’t pick up an object that far, right? Right! Now imagine the area in the middle. If the 36 kHz could pick objects in the far area, it is certain that it picks up any objects closer. When we are close at 40 kHz we begin to lose any sights of objects. So approx. the 40 kHz means the middle zone. At the zone just in front of the vehicle, objects are detected by all three frequencies. So we can now tell the robot to act alike.

If an object exists in the far zone, the robot wants to come closer. If the object is just in the middle, the robot likes it. So it stays just where it is. But our robot is afraid of objects… They may trigger its interest but it likes to keep its distance. So if the object is in the every frequency zone, it heads back…

Although object tracking is an interesting code, mine didn’t work well… I hope to fix that. In the existing code the objects acts well in the middle zone but it doesn’t act well in other zones. Not a matter… The point of LASER robot wasn’t just an object tracking code… You will see this later.

So what’s the point of this robot? Well, no more than code demonstrating. This robot was meant for a robot exhibition here in Greece at Athens Digital Week. So what do we demonstrate here? Microcontrollers. Yup! That’s right!!! What I haven’t reveled so far is that the robot uses two microcontrollers… Oh, yup!!! Two ATMEGA8535! And if so, how the whole show runs??? Well…. One microcontroller acts like HID (Human Interface Device) and the other microcontroller acts as SVC (Slave Vehicle Controller). Yeah, you think we got something here??? No… I’ll explain…


The two microcontrollers “talk” to each other via UART. What they say? How nice the weather is!!! I don’t fool you… please... Of course it can’t tell if it’s a lamp or the sun but that’s all right… It’s some basic photoresistors of course… Well, one of the functions embedded to the robot is to measure the sunlight (or just light) on its photoresistors.  One microcontroller is tasked to measure the light constantly on both photoresistors, compare their value and sent a string of data to the other… The string contains data which tell the robot where to turn to… The locomotion of the robot is based on two servos… So yes, the other microcontroller takes the data and produces two PWM signals which are made in software and not based on a hardware timer or PWM signal.

This is about robot tracking light… But the robot is also able to move its turret towards the light based on these photoresistors. For now light tracking is made in two dimensions. As soon as I get an extra photoresistor I’ll add it to the code.

But today, there is so much word about energy! Energy here, energy there, they make USB batteries (which cost to build is much higher than a simple NiMH and its charger… sorry I had to tell), they say we have global warming and all that……………. which I find at least funny……. Ok, I think we sidetracked a lot. The mean of all these was that I have included the DS2438Z IC to the party. The HID microcontroller is responsible for it. If you want details on the IC feel like paying a visit to the other site I wrote, Smart Battery Monitoring – DS2438Z. What we do with the IC? We measure the voltage of the battery of course.  When the battery drops below a desired voltage level, I immediately stop using the servos. But this only happens in free roaming mode… Free roaming mode? Mode? What?

Well, it’s true that constant reprogramming not only is hated my EEPROMs but also from programmers. And in our case, where this robot is supposed to be exhibited, the least you want is to reprogram it constantly in front of people… Not a chance! Period!

So how do we solve this problem. Well, you can embed many programs in one bigger!!! And how we change the programs in between. Well, that’s easy! First of all we got a 4x20 LCD to display whatever information about the programs. Still we need interface. Well, please, don’t expect much. I just use simple push buttons, in general IO, I didn’t want to get involved with interrupts because the tent to mess with LCD displays. So, there are three buttons, the one you can enter/enable and other two which are for up and down!!! Don’t be surprised if I tell you that all these are made by the HID microcontroller!

Now, let’s have a little talk about the modes. Mmm, many modes and that is good for an instance! Well, let’s name them: 

1.       Turret light tracking and rest of the robot still

2.       Select roaming modes

a.       Track light (photovore)

b.      Basic IR roaming with DS2438Z low battery hold

c.       Follow object (still the code needs work

3.       Free Roaming with IR and turret tracking light on its own will

4.       DS2438Z battery monitor report, anything else off

So here are all the modes, luckily for me I have explained them above!!!


Oh, I just forgot to mention… No Sonar… Scans take too long to consider it an active sensor so I keep it for something else!!! Well, this something is coming soon too! Yup, I’m high!!!

How to Smart Battery Monitor - DS2438

First of all, I DO NOT advertise any product, but I was really amased by capabilities of the IC...


Just an idea on what we will be working on... Enjoy the vid!!!


The DS2438 is a everything about the battery IC... It can measure Voltage of the battery and record it, it can measure current in and out the battery pack... It can also measure the temperature of the pack... And also has some spare EEPROM room... A all in one chip!!! The communication protocol is 1wire so, I was not just getting the readings directly... In 1wire communication many devices can exist on the same bus. So the microcontroller has to address every device seperatelly... How? Every 1wire device has it's special ID number which is laser in the ROM. In order to select a device, you firstly have to reset the device with a pulse. After that the device repplies with a presence pulse. Then the master has to choose a ROM function. There are 4 ROM functions in every 1wire device...

The READ ROM function which makes the device to sent the lasered ID to the master and then the master gains access to the CONTROL functions of the device.

The MATCH ROM function in which the master sents out 64bit. If all these 64bit match the device special ID then the device is accessible to the control function. If not, the device is inaccessible.

The SEARCH ROM function. Actually I have never used it, so I may be mistaken. When selected this function makes the device to sent 128bits. These 128bits are actually the 64bits of the lasered ROM. But how can they be double??? The device firstly sents two bits, the first with the TRUE value of the first bit of the lasered ROM (64bit all together) and the other with the FALSE value. Then the master must sent a bit back to the device. If this bit matches the first one of the lasered ROM, then this continues with the next bit and so on until the last bit matches. If so the device is further accessible. If not the device is inaccessible.

The SKIP ROM function, which just skips the ROM and gives you access in the CONTROL functions of the device.

And here is the flowchart from the .pdf of the device:


If multiple device exist on the same bus, only the MATCH ROM function is recommented. Otherwise, a data collition may occure resulting to wrong data.


Now, that was some general info on accessing a 1wire device. Let's say we have a DS2438Z and we have accessed the CONTROL functions of the device. What next???

Most 1wire devices uses a scratchpad to communicate with the master. Whatever the master wants to say to the device writes it on the scratchpad and reverse. So controlling the scratchpad of the device in co-operation with the EEPROM, It's the most important thing in the DS2438Z (plus every 1wire device).Here are the basic functions with the scratchpad: (when I refer to SP it's scratchpad)

NOTE: In DS2438 the scratchpad consists of 8 pages. The right address for every page is from h00 to h07


The WRITE SP function. In this function the master sents the number of the page of the SP
and then the master sents bytes of data. The maximum number of bytes is 8. If the 8th byte is reached the deviced must be reseted by the master.

The READ SP function. In this function the master sents the number of the page it wants to read and then the device starts to sent the 8 bytes each page has. If the 8th page is reached then the deviced must be reseted by the master. The reading can be ended prematurely by having the master to reset the device.

The COPY SP function. In this function the master sents the number of the page of the scratchpad which will be copied to the respective page of the EEPROM of the device. When this action ends the device must be reseted by the master.

The RECALL MEMORY function. In this function the master sents the number of the page of the EEPROM of the device that will be copied to the respective page of the scratchpad. It's the reverse action of what COPY SP function does. When this action ends the device must be reseted by the master. The RECALL MEMORY function is nessessary for reading the data out of the device. When the device measures current, voltage or temperature it stores the data in the first page (h00) of the EEPROM. Cause the master can only read the scratchpad, we have to copy the data from the EEPROM to the scratch pad. And this is what we do here.

Now in order to have new data available (temperature, voltage, current) be must tell the device to convert the respective readings to data. Current readings are automatically converted every 27msec. But we must tell the device to convert the temperature and the voltage readings. This is done with the following functions:

The CONVERT T function. With this function we convert temperature readings to data. When this ends the device must be reseted.

The CONVERT V function. With this function we convert voltage readings to data. When this ends the device must be reseted.

Note that with every function that consist of the CONTROL functions we can reset the device prematurly. This DOESN'T mean that the function will not take place. Every action that must be done will be done no matter if reset occures. Here is the flowchart of the CONTROL functions:



Now let's get a little bit more practical - enough with theory. The DS2438 -yes- it can measure voltage and sent it to us. But, from where does it gets the voltage readings? Mama - Dallas has configured the chip to read the voltage on VDD pin, but we want to use the VAD pin, which is connected straight to the source (the battery pack). So we must reconfigure it. How? Guessed right... back to theory.

The most important page of the scratchpad of the DS2438Z is page h00 or the first page. The page looks like that, it's important to note:




This page like the others consists of 8 Bytes of data. The first Byte which is the most important to us is the Status byte. It tell us whether a convertion is over ( voltage(ADB) - temperature(TB) ), if the copy from scratchpad to EEPROM is done(NVB) ( COPY SP function) and the on board configuration.

If the IAD is setted to 1 then current measurements will be taken at the rate of 36.41Hz.

If the CA is setted to 1 then the current accumulator is working. The current accumulator shows as how much current is going in and out the battery pack, but I won't be using it probably in this tutorial so I won't get any further.

If the EE is setted to 1 then the current accumulator data is saved on the EEPROM of the chip, we are not intrested in here neither.

And finally what we wanted from the begining. The AD status bit. This bit if set to 1 the voltage measurement are taken on the Vdd pin. If setted to 0 then the current measurements are taken on the Vad pin.

So we must set this bit to 0. How? Well the first byte of the first page of the DS2438Z looks like this:




The acronames were displayed previously.


Now, X, ADB, NVB, TB can NOT be written, although if mistake occurs that doesn't mean you lose the device. We can only write bits AD,EE,CA,IAD.

In it's binary form this byte comes from mama - Dallas like that:

00001111 or in HEX h0F


We want to convert this to:

00000111 or in HEX h07


So here is what we'll do in a simples flowchart - table (everything which has a h infront of it is hex code)


1. We reset the device.

2. The device repplies with a presence pulse.

3. We write to the device hCC which is the SKIP ROM function in order to access the IC immediately.

4. We write to the device h4E to issue a write to scratchpad function.

5. We write to the device h00 to select the page of the scratch pad.

6. We write to the device h07 which is the first byte we wanted to write to the device as shown.

7. We reset the device to end the write sequence prematurly.

8. The device repplies with a presence pulse.

9. We write to the device hCC to SKIP ROM.

10. We write to the device h48 to issue a Copy SP to EEPROM command. This way we store the changed

byte we previously sent.

11. We write to the device h00. This determines the page of the SP copied to EEPROM, we changed page h00

or the first page of the device so this is the one to copy to the EEPROM.

12. We wait 20msec. The Copy SP to EEPROM usually takes 10msec, but just to be secure we wait a little bit


13. We reset the device.

14. The device sends back a presence pulse.

15. We write to the device hCC to SKIP ROM.

16. We write to the device hBE to issue READ SP command.

17. We write to the device h00 to determine the page of the scratchpad we want to read. (h00 - first page)

18. The device sents as 9 bytes. The 8 Bytes of page h00 and a CRC (don't care).

19. We see if Byte 0 matches our pattern (00000111). If yes we are ok!!! If not, you did something wrong.

Of course as soon as you read the first byte do don't have to read the others. So you can issue the reset

earlier if you like.

20. The device after the reset sends a presence pulse. We are done!!!


Now we have the device ready and set!!! Nice!!! But since reality is not that theoritical let's see how the code is like. I program in BASIC. It's a easy to learn language which is based on structured programming, so by learning basic which is a enter to Matrix language, you can easier go to C, C++ and other languages that are based on structured programming. Since I use the ATMEL family microcontrollers, I use the BASCOM compiler to write my programs. The free version supports 4 kB of code, which is enough for us now. You can find it at:


I assume you 've already have installed and opened the program. Here is the part of code we need to write to reconfigure the device as shown above.


' --------[ Device Config]----------------------------------------------------

$regfile = "m16def.dat"
$crystal = 16000000
$baud = 9600

' --------[ LCD Config ]------------------------------------------------------

Config Lcdpin = Pin , Db4 = Portc.3 , Db5 = Portc.2 , Db6 = Portc.5
Config Lcdpin = Pin , Db7 = Portc.4 , E = Portc.1 , Rs = Portc.0
Config Lcd = 20 * 4

' --------[ Port Config ]-----------------------------------------------------

Config 1wire = Portb.2

' --------[ Variable Config ]-------------------------------------------------

Dim I As Byte , W As Byte
Dim Crc As Byte
Dim Table(3) As Word
Dim Status As Byte
Dim Thresh As Byte
Dim Reg_no(8) As Byte

' --------[ Characters ]------------------------------------------------------

' --------[ Initialize ]------------------------------------------------------

' Start

' At first we reset the device.
' The ATMEGA automatically receives the presence pulse and ignore it.
' Then we skip the ROM.
1wwrite &HCC
' Then we issue the write to SP command.
1wwrite &H4E
' And now we select the page of the scratch pad we want to write in.
1wwrite &H00
' Since we want to write on one byte we sent only one.
1wwrite &H07 ' 07 = 0000 0111
' And we issue reset to stop the write sequence.
' And the device gives as back a repply, we ignore it.
' And we skip ROM again
1wwrite &HCC
' Now we copy what we writed on the scratchpad in the EEPROM to secure the data.
1wwrite &H48
' And we select the page to be copied.
1wwrite &H00
' We wait some time so the whole thing is over.
Waitms 50
' And we reset the device.
' Again we ignore the presence pulse.
' And now we read what we wrote... if correct we are ok!!!
' First skip the ROM
1wwrite &HCC
' Then we issue the read scratchpad function
1wwrite &HBE
' And we select the page we want to view
1wwrite &H00
' And now... start sucking data baby!!!
' Note: Status is a variable.
Status = 1wread()
' And we see what we have done in binary form!!!
Lcd Bin(status);
' If it's 00000111 we are free to go!!! Nice doing!!!
End 'end program
' Don't forget the End command and leave the processor hanging....


Now let's get a program to enable us to read the temperatute, the voltage and the current reading. DS2438 doesn't write all these data on the scratchpad, but on the EEPROM. That's why we have to recall the memory, to have the data written on the scratchpad so the master can read 'em. REMEMBER a 1wire device and a microcontroller (master in general) can only communicate through the scratchpad. So we first issue a RECALL MEMORY function and then we read the scratchpad... So here is the program:

' --------[ Device Config]----------------------------------------------------

$regfile = "m16def.dat"
$crystal = 16000000
$baud = 9600

' --------[ LCD Config ]------------------------------------------------------

Config Lcdpin = Pin , Db4 = Portc.3 , Db5 = Portc.2 , Db6 = Portc.5
Config Lcdpin = Pin , Db7 = Portc.4 , E = Portc.1 , Rs = Portc.0
Config Lcd = 20 * 4

' --------[ Port Config ]-----------------------------------------------------

Config 1wire = Portb.2

' --------[ Variable Config ]-------------------------------------------------

Dim I As Byte , W As Byte
Dim Crc As Byte
Dim Temp1 As Byte , Temp2 As Byte
Dim Tempfix As Word
Dim Table(2) As Word
Dim Status As Byte , Thresh As Byte
Dim Reg_no(8) As Byte

' --------[ Initialize ]------------------------------------------------------

' Main routine - The program will be executed continiously
Cursor Off
Lcd " DS2438Z Report "
' Reset the device.
1wwrite &HCC
' Issue temperature convertion.
1wwrite &H44
' Each temperature convertion takes 10msec, better wait.
Waitms 10
' Reset the device.
1wwrite &HCC
' Issue voltage convertion.
1wwrite &HB4
' Each voltage convertion takes 4msec, better wait.
Waitms 5
' Reset the device.
1wwrite &HCC
' Issue a RECALL MEMORY function on page h00.
1wwrite &HB8
1wwrite &H00
' Reset the device.
1wwrite &HCC
' Issue a READ SP function on page h00.
1wwrite &HBE
1wwrite &H00
' Start sucking data
Status = 1wread()
Temp2 = 1wread()
Temp1 = 1wread()
For I = 1 To 2
Table(i) = 1wread(2)
' Display temperature
Lcd " Temp: "
Shift Temp2 , Right , 3
Lcd Temp1
Lcd "."
Tempfix = Temp2 * 3125
If Temp2 > 32 Then
Lcd "0"
Lcd Tempfix
Lcd Tempfix
End If
' Change line
' Display current measurements
Lcd " Drain: "
' Here I multiply current readings with 66.7
' This number represent an equalation based on the
' sense resistor. Please refer to the datasheet for more info.
Table(2) = Table(2) * 66.7
Lcd Table(2)
Lcd " mA "
' Change line
' Display voltage measurements
Lcd " Voltage: "
Lcd Table(1)
Lcd " mV "

Loop ' Loop

End 'end program


And the results of the program. A image can be very rewarding:


Note that in the picture voltage is at 4.35V. I was just too lasy to fix this..... sorry...


Ok, by now you should know how to operate the DS2438. Here is the schematic suggested by me for the device.For Rsense refer to the datasheet. The bigger the value, the less current can be measured, but the higher the sensitivity is. Enjoy:



Hope, you have understand the basics of the DS2438Z IC for more informations and functions refer to the datasheet. It would also be very nice to hear from you. IMPORTANT: I'm NOT an engineer, just a high school student, so my help is strictly limited. E-mail at: [email protected] but please don't expect I'll build or test anything for you. I can only answear to some basics!!!