Lego NXT Hitechnics Sensors

freddy2

New Member
I have just updated my program editor software and see that someone has added sample files for communicating to hitechnics sensors. This would be brilliant for our Robocup Soccer robots and I will give it a go when we return to school in a few weeks. Does anyone know how to get in contact with the author of the sample files for a few helpful hints?
 

hippy

Technical Support
Staff member
Welcome to the PICAXE Forum.

The example files were produced and added by ourselves, Rev-Ed. We will also be providing a datasheet / tutorial and offering other support for a variety of NXT Sensors. In the meantime this forum is probably the best place to ask questions.

Lego NXT sensors use I2C for command and control so they are fairely straight forward to interface to.
 

NXTreme

Senior Member
At least, most of the Lego sensors use I2C. The ones that don't are the light sensor, touch sensor, sound sensor and the new color sensor. You still could interface these with the Picaxe, but it wouldn't be with I2C.
 

Technical

Technical Support
Staff member
... and the new color sensor. You still could interface these with the Picaxe, but it wouldn't be with I2C.
The Lego RGB sensor is not i2c, but the Hitechnic RGB color/colour sensor 2 does indeed work with i2c, so this is a good RGB colour sensor to use.
 

freddy2

New Member
thanks to all who responded. The Robocup soccer copmpetition has moved to pulsed balls and atm the hitechnics IRseekers V2 are the only working sensor option. We have never used I2C on the PICaxes before but the sample programs look pretty easy. As per usual the devil will be in the detail and the soccer robots are a pretty extreme programming environment. The robots will use the IRSeeker to find the ball, a HT compass for direction and some student manufactured LDR potential dividers for position on the field. If someone has looked at the IRseeker interface I would welcome assistance but will attempt tp pull together something based on the HT Color sensor and Compass samples. If I can post it here for review that would be great.
 

freddy2

New Member
first stupid questions ........

I want to run a HT compass and a HT IRSeeker simultaneously.

1. I have the standard address for the IR Seeker at 0X10 = $10 (read) and $11 (write). Can't find equivalent on HT site for Compass module. The sample files show same address for all HT examples. Is this correct ? Is the adress set by the master (PICaxe)?

2. How do I set mode for IR Seeker to AC (write 41H = what?) - or is it not needed?

..... probably more to come
 

freddy2

New Member
Just as an aside.

I've just looked at Lego's NXT shop.
One example of many:
http://shop.lego.com/ByTheme/Product.aspx?p=9841&cn=17

Beautifully manufactured,
and I'm sure it works superbly,

but, the price to build a robot !!!!

I'm glad I don't have
young, intelligent, technologically orientated,
but economically naive kids.

e
The stuff is brilliant (and a lot cheaper if you purchase from the US). I teach engineering at a Australian high school and we use the NXT from 12 year old students onwards. Very hard wearing and simple for beginners but with a high ceiling as the students mature. It was great to see one of our teams use NXT (programmed in RobotC) + Hitechnics sensors to finish 3rd in the open category at the Robocup Nationals. The PICaxe soccer robots we build are faster and find the ball better but are much more fragile - if we can get the ht sensors to work on them they will be awesome. Great fun.
 

NXTreme

Senior Member
1. I have the standard address for the IR Seeker at 0X10 = $10 (read) and $11 (write). Can't find equivalent on HT site for Compass module. The sample files show same address for all HT examples. Is this correct ? Is the adress set by the master (PICaxe)?

2. How do I set mode for IR Seeker to AC (write 41H = what?) - or is it not needed?
I don't quite understand Q. #1 but let me try explaining a bit. I don't want to make it overly simplistic. but better to start at the beginning :). The HT IR Seeker V2 has various registers and depending on what your trying to read from it (IR Beacon direction, AC or DC signal strength ect.) you read a different register. Thus, it doesn't have one set register from which to read everything.

The Compass sensor is different. Since your only reading back one value, a compass heading, you can fit it all in two bytes, or two registers. Those are registers 0x42 and 0x43. Then, with a bit of simple math, you can convert that to a word variable sized heading value. Or, if you don't need a very accurate heading, you can just use register 0x42 for a rough value. There are other registers for calibrating the sensor and such but they aren't needed for basic testing.

If you want to use both sensors at a time on one I2C port, you will need to change the address. I'm not exactly sure how, there were instructions on the old NXTasy.org forums but it's gone now. I'd suggest asking on the MindBoards.net forums. I'm a somewhat regular visitor and most of the folks over there are as happy to help as the people here. Just ask nicely and don't expect them to do your homework and you'll come out alive ;).

To change the the IR Seeker to AC mode you will have to write something to register 0x41, I think. Again, I'm not sure how/what but the guys at MindBoards will know. You could probably find the answers looking through the various code samples available online (HT website, RobotC forums ect.) but you'd may as well ask two questions for the price of one.

I probably didn't answer your questions very well, but if I can help in any other way, just say so. The people on the Picaxe forum here are quite knowledgeable but if you have more detailed questions about the NXT sensors, why not ask on the NXT forum :). One of the moderators over there is in to Arduino as well, so they're not set against other uControllers or robot brains. Have fun hacking this together, I hope all goes well and that your team does well during the next round of RoboCup!
 

NXTreme

Senior Member
The Lego RGB sensor is not i2c, but the Hitechnic RGB color/colour sensor 2 does indeed work with i2c, so this is a good RGB colour sensor to use.
Yes, sorry, I didn't specify which new color sensor. Thanks for clarifying.

Just as an aside.

I've just looked at Lego's NXT shop.
One example of many:
http://shop.lego.com/ByTheme/Product.aspx?p=9841&cn=17

Beautifully manufactured,
and I'm sure it works superbly,

but, the price to build a robot !!!!

I'm glad I don't have
young, intelligent, technologically orientated,
but economically naive kids.

e
I agree with you, beautifully made, works well even if the graphical programming language it comes with is a bit clunky at times. I also agree on the price. It's priced at a much higher level than your average kid with your average allowance will make in a year or even two. If you buy the whole kit your getting a pretty good value but if you buy the parts individually it can get expensive.

The nice thing about it though, is that you can just dive in and have fun. The quick start robot is a great way to get something "just working" in under an 45 minutes and the other included instructions are a great way to learn the basics. I had a couple reasons I choose the NXT as my entry in robotics/electronics.

  • I had lots of Lego and Technic pieces already, which meant I could build almost anything, if only I used my imagination.
  • It came with a graphical programming interface, which made it a lot less daunting to just jump in and learn the basics of programming, without having to worry about correct syntax and the like.
  • I didn't have to worry if the motor drivers where being overloaded, the resistors the wrong value or if I had enough power supply smoothing to keep the thing from constantly rebooting, it just "plugs and plays".

While these things may seem a bit simple now, when your beginning it can be a bit hard to grasp the basics. Now that I have gotten past what a variable is and how to wire an LED, I actually prefer the Picaxe over the NXT for pretty much everything except for one thing, the fact that I can build and program a basic "start here" robot in half an hour.

Don't want to start a war, just pointing out a few things :). Oh and sorry to go OT...
 

freddy2

New Member
Yes, sorry, I didn't specify which new color sensor. Thanks for clarifying.


I agree with you, beautifully made, works well even if the graphical programming language it comes with is a bit clunky at times. I also agree on the price. It's priced at a much higher level than your average kid with your average allowance will make in a year or even two. If you buy the whole kit your getting a pretty good value but if you buy the parts individually it can get expensive.

The nice thing about it though, is that you can just dive in and have fun. The quick start robot is a great way to get something "just working" in under an 45 minutes and the other included instructions are a great way to learn the basics. I had a couple reasons I choose the NXT as my entry in robotics/electronics.

  • I had lots of Lego and Technic pieces already, which meant I could build almost anything, if only I used my imagination.
  • It came with a graphical programming interface, which made it a lot less daunting to just jump in and learn the basics of programming, without having to worry about correct syntax and the like.
  • I didn't have to worry if the motor drivers where being overloaded, the resistors the wrong value or if I had enough power supply smoothing to keep the thing from constantly rebooting, it just "plugs and plays".

While these things may seem a bit simple now, when your beginning it can be a bit hard to grasp the basics. Now that I have gotten past what a variable is and how to wire an LED, I actually prefer the Picaxe over the NXT for pretty much everything except for one thing, the fact that I can build and program a basic "start here" robot in half an hour.

Don't want to start a war, just pointing out a few things :). Oh and sorry to go OT...
If you haven't already tried it RobotC on NXT is awesome. Very potent stuff. The Lego graphical language has the worst debugging I have seen for many years and can get very frustrating!!! Back on topic I will post up some BASIC code for (hopefully) reading IR seeker and compass soon for comment. All looks easy in isolation but I am having trouble getting my head around multiple devices - not as intuitive as RobotC approach. Hmmmmmm.
 

Technical

Technical Support
Staff member
first stupid questions ........

I want to run a HT compass and a HT IRSeeker simultaneously.

1. I have the standard address for the IR Seeker at 0X10 = $10 (read) and $11 (write). Can't find equivalent on HT site for Compass module. The sample files show same address for all HT examples. Is this correct ? Is the adress set by the master (PICaxe)?

2. How do I set mode for IR Seeker to AC (write 41H = what?) - or is it not needed?

..... probably more to come
The IR seeker address is $10, the compass $02. these default values are set at manufacture, and documented on the Hitechnic web pages. They are different so no problems using both on same i2c bus.

The IRseeker has both dc and ac values in different registers. dc is from $42 upwards, ac from $49 upwards. You need to write to register $41 to change mode, we guess from this abstract sentence on the Hitechnic page

Code:
This plug selects the mode the sensor is operating in, 0 = Modulated Mode (AC), 2 = Un-modulated (DC) mode.
that the values are 0 (AC, default) and 2 (DC)

Untested as we don't have an IR seeker v2, but this should give you some help on using both together

Code:
#picaxe 20x2
#no_table
' PICAXE-20X2 sample program for two Hitechnic NXT Sensors
' Hitechnic NXT Compass Sensor (part MS1034)
symbol heading_2degree = b0
symbol heading_adder = b1
symbol heading = w1 
 
' Hitechnic NXT IRSEeker V2
symbol ac_direction = b10
symbol sensor1_ac = b11
symbol sensor2_ac = b12
symbol sensor3_ac = b13
symbol sensor4_ac = b14
symbol sensor5_ac = b15
 
' Wires on NXT jack plug.
' Wire colours may vary. Pin 1 is always end nearest latch.
' 1 White No connect
' 2 Black 0V
' 3 Red 0V
' 4 Green V+ (4.5V or 5V)
' 5 Yellow SCL
' 6 Blue SDA
' Do not use i2c pullup resistor - already provided within sensor.
 
init:
 'Slave address of compasss is $02
 'Slave address of IRSeeker2 is $10
 hi2csetup i2cmaster, $02, i2cslow, i2cbyte
 
 'Optional configuration of compass
 hi2cout [$02],$41,($00)  ; normal mode
 ;hi2cout [$02],$41,($43) ; hard iron calibrate mode
 pause 100
 
 'Optional configuration of IRSEEKER2
 hi2cout [$10],$41,($00)  ; normal AC mode
 ;hi2cout [$10],$41,($02) ; uncalibrated DC mode
 pause 100
 
main:
 ; read values from compass (address $02)
 hi2cin [$02], $42,(heading_2degree, heading_adder) 
 
 heading = heading_2degree * 2 + heading_adder
 
 ; read values from IRSeeker  (address $10)
 hi2cin [$10], $49,(ac_direction,  sensor1_ac, sensor2_ac, sensor3_ac, sensor4_ac, sensor5_ac) 
 
 ; debug display
 debug
 ; wait 1 second then loop
 pause 1000
 goto main
 
Last edited:

NXTreme

Senior Member
If you haven't already tried it RobotC on NXT is awesome. Very potent stuff. The Lego graphical language has the worst debugging I have seen for many years and can get very frustrating!!!
Yes, what you say is true. The graphical language it comes with is nowhere near even being "good" but it taught me beginners programming and I still like using it now and then. Also trying to learn NXC...

Oh, one other thing. The sensors include pullup resistors built in, by using two on the same bus you halve the resistance. If you have problems you might need to find a way of isolating each sensor from the other or something like that. The recommended pullup value is 82K and I think that's what the sensors have built in but I have used values down to about 10K with no problems. YMMV
 

freddy2

New Member
Thanks heaps T. I was 90% of the way there but was suffering from USE (Unidentifiable Syntax Errors). Modified program to be similar to yours and all looks good. When I get back to school I will set up some quick tests and report back to the forum. Will need to watch for possible resistor issues as per NXTreme's post. Just out of interest (I am not too hot at BASIC) what is the purpose of the statements #picaxe 20X2 and #no_table. (Will be 28X1 or 28X2 for our bot). Are they specific to X2s ?
 

freddy2

New Member
Thanks NXTreme. I will try it out when I get back to school. I am guessing that a resistor in series to each sensor would restore overall pullup. Do you know value of existing pullup in sensors ?
 

NXTreme

Senior Member
I'm not sure that you would be able to increase the pullup value without taking apart at least one sensor. However, it should work in parallel. There was someone that ran some tests and figured out that 33K pullups were actually better than the 82K pullups already installed. Thus, when I make my own I2C sensors, I usually use something in between 82K and 33K. If you need more details on the internals of NXT sensors, check out the HDK on the Mindstorms website.
 
Last edited:

Technical

Technical Support
Staff member
There is no need to worry about the pullup values. The NXT sensors use the weird 82k value because there is already a pullup resistor circuit within the NXT itself. Therefore when the NXT sensors is used with the Lego NXT programmable brick the combined parallel resistance (from both ends of the cable) becomes around the normal value (between 5-25k).

Therefore using two NXT sensors in parallel without the NXT is absolutely no problem, as long as they both have different slave addresses.
 

Technical

Technical Support
Staff member
what is the purpose of the statements #picaxe 20X2 and #no_table. (Will be 28X1 or 28X2 for our bot). Are they specific to X2s ?
#picaxe tells the compiler which chip is in use (28X2 would also be fine), #no_table just speeds up the download. See 'Directives' in part 2 of the manual.
 

freddy2

New Member
The Compass works but .......

Finally got time to try out the program. I am using a PICaxe AXE020 Project Board with a 28X1 processor. I needed to remove the 10K pulldown resistors to get it to run. See attached picture for more info.

The compass works perfectly (and swaps happily between cables) but there seems to be a possible addressing issue with the IRseeker(?). Debug only returns 255 for all registers. See attached screen dump.

I am running your code with minor mods for 28X1 in directives. I have tried a number of sensors, with and without ball, swapping sensor with compass etc and hardware looks OK. V1 is 4.65V so there is probably enough "juice". In desperation I tried binary address and 7-bit equivalents with no joy. Looked at Hitechnics web and also Xander's drivers for RobotC and all looks good.

Any suggestions?
 

Attachments

freddy2

New Member
FSOC - Here is the program

Code:
#picaxe 28x1
#no_table
' PICAXE-28X1 sample program for two Hitechnic NXT Sensors
' Hitechnic NXT Compass Sensor (part MS1034)
symbol heading_2degree = b0
symbol heading_adder = b1
symbol heading = w1 
 
' Hitechnic NXT IRSEeker V2
symbol ac_direction = b10
symbol sensor1_ac = b11
symbol sensor2_ac = b12
symbol sensor3_ac = b13
symbol sensor4_ac = b14
symbol sensor5_ac = b15
 
' Wires on NXT jack plug.
' Wire colours may vary. Pin 1 is always end nearest latch.
' 1 White No connect
' 2 Black 0V
' 3 Red 0V
' 4 Green V+ (4.5V or 5V)
' 5 Yellow SCL
' 6 Blue SDA
' Do not use i2c pullup resistor - already provided within sensor.
 
init:
 'Slave address of compasss is $02
 'Slave address of IRSeeker2 is $10
 hi2csetup i2cmaster, $02, i2cslow, i2cbyte
 
 'Optional configuration of compass
 hi2cout [$02],$41,($00)  ; normal mode
 ;hi2cout [$02],$41,($43) ; hard iron calibrate mode
 pause 100
 
 'Optional configuration of IRSEEKER2
 hi2cout [$10],$41,($00)  ; normal AC mode
 ;hi2cout [$10],$41,($02) ; uncalibrated DC mode
 pause 100
 
main:
 ; read values from compass (address $02)
 hi2cin [$02], $42,(heading_2degree, heading_adder) 
 
 heading = heading_2degree * 2 + heading_adder
 
 ; read values from IRSeeker  (address $10)
 hi2cin [$10], $49,(ac_direction,  sensor1_ac, sensor2_ac, sensor3_ac, sensor4_ac, sensor5_ac) 
 
 ; debug display
 debug
 ; wait 1 second then loop
 pause 1000
 goto main
Any ideas on what to try next would be welcome.

Thanks
 

hippy

Technical Support
Staff member
I'd trim the code back so it only accesses the Lego NXT IR sensor, not the compass as well, disconnect the compass from the I2C bus.

I'd also post any links to the NXT IR sensor and other code for it you have / can find so it saves others searching for information on it, giving up and not being able to help if they cannot find it.
 

mattallen37

New Member
I'm not sure that you would be able to increase the pullup value without taking apart at least one sensor. However, it should work in parallel. There was someone that ran some tests and figured out that 33K pullups were actually better than the 82K pullups already installed. Thus, when I make my own I2C sensors, I usually use something in between 82K and 33K. If you need more details on the internals of NXT sensors, check out the HDK on the Mindstorms website.
Today, I was able to get the NXT to work with resistors in the 10k to 470k range.
There is no need to worry about the pullup values. The NXT sensors use the weird 82k value because there is already a pullup resistor circuit within the NXT itself. Therefore when the NXT sensors is used with the Lego NXT programmable brick the combined parallel resistance (from both ends of the cable) becomes around the normal value (between 5-25k).

Therefore using two NXT sensors in parallel without the NXT is absolutely no problem, as long as they both have different slave addresses.
Actually, there isn't any pullups circuit in the NXT. There are protection resistors in series with the IO lines of the ARM7, but not pullups. Based on my tests today, the NXT was able to communicate with a 20X2 with resistors from 10k-470k.

Probably 6 or 8 NXT I2C sensors is about the limit (about 10k-13.5k being in parallel), IF they all have 82k pullups (and different addresses). I think all but one of the HiTechnic sensors has the same I2C address (0x02, except for the "IR Seeker v2", which is 0x10). Many of the mindsensors products have a user selectable I2C address, specifically for this reason.

The documentation for slow/fast speed (ac / dc??)
is not clear, but this page suggests you need to send 0/1 instead of 0/2
http://bricxcc.sourceforge.net/nbc/nxcdoc/nxcapi/group___h_t_i_r_seeker2_constants.html

to address $41
IIRC, that is for the speed at which it looks to filter light. It is selectable to either 60hz or 50hz, depending on where you live (the frequency of mains power). I could be wrong, but I think that is what it is for. I am almost certain that the color sensor v2 (maybe v1 as well) has that ability. Looking at that page, it suggest, that it is for the modulated IR speed, either 600 or 1200 hz. I am not sure, but it is likely one of the two.
 

Technical

Technical Support
Staff member
Actually, there isn't any pullups circuit in the NXT. There are protection resistors in series with the IO lines of the ARM7, but not pullups. Based on my tests today, the NXT was able to communicate with a 20X2 with resistors from 10k-470k.
Actually there most definately are! Look at page 3 of the NXT motherboard schematic from the hardware developer kit.

There is an 82k pull up on each data pin (labelled resistors R112 to R119) as well as a 4.7k series resistor and a 1M pull down. The combined action of all the components interacting together gives an equivalent i2c pullup value of less than 82k, which is fine for i2c devices to work. However it is certainly not a 'standard' i2c layout!
 

mattallen37

New Member
Actually there most definately are! Look at page 3 of the NXT motherboard schematic from the hardware developer kit.

There is an 82k pull up on each data pin (labelled resistors R112 to R119) as well as a 4.7k series resistor and a 1M pull down. The combined action of all the components interacting together gives an equivalent i2c pullup value of less than 82k, which is fine for i2c devices to work. However it is certainly not a 'standard' i2c layout!
Look again. They are labled "NM" (not mounted).

I wonder, do you have an NXT, and have you tried interfacing with the I2C pins? Have you seen the PCB? You would learn a lot by doing so.
 

Technical

Technical Support
Staff member
Yes, we have used a 20X2 with a NXT numerous times, and have done numerous experiments with using both a 20X2 as a simulated slave (e.g. replacing an ultrasonic sensor with the PICAXE on an NXT brick) and using a 20X2 as master (ie NXT brick replacement) with numerous Lego/hitechnic sensors. You will see a number of tested NXT example files in the /samples folder of the Programming Editor software. There are also lots of schools doing this type of thing with robocup type robots, merging PICAXE chips in with Lego sensors / NXT bricks.

In each case it worked fine without any hacking.

What we wanted to stress here is
a) no NXT motherboard hacking is required at all
b) the recommended Lego external pullup resistor (inside sensor) of 82k works fine in all PICAXE scenarios
c) When using a Lego i2c sensor with a 20X2 master (e.g ultrasonic, i2c temperature or hitechnic color) there is no need to add pullups to the PICAXE circuit, as the pullups in the sensor work fine
d) When faking a NXT sensor (ie using the PICAXE as a slave to simulate the ultrasonic sensor) it works fine by adding 82k pullup resistors to the PICAXE system.

We see no reason to move away from what the Lego NXT recommendations are, they work! The circuitry inside the NXT is complex, linking the inputs to numerous non-standard i2c circuits and so where possible we recommend you just stick to what is known to work.

We have not opened the NXT as there has been no need, but even if the pullups are not fitted what is very clear is that there are a lot of elements on the i2c bus which are non standard such as diodes, series 4k7 resistor and pullup/down resistors (- e.g. having two diodes, 4k7 series resistor and 1M pull down resistor on an i2c line is extremely unusual). However in all cases the recommended circuit (82k pullup in the sensor) worked first time in our PICAXE experiments.
 

freddy2

New Member
Thnx Hippy. I have commented out all the compass code and tried all possible combinations of physical connection and sensor. The fact that I am getting no return (assuming this is what 255 indicates - it remains the same when the sensor is unplugged) from the IRseeker looks suspicious. Even if the incorrect mode is selected I should still be getting something back to tell me it can't find the ball.

The default settings should work fine.

We have used the sensors in the past using NXT + RobotC with some custom drivers so we have a bit of experience with them. Here is the link to the Hitechnics site

http://www.hitechnic.com/cgi-bin/commerce.cgi?preadd=action&key=NSK1042

All looks good there. Also our register addressing is the same as used in the RobotC drivers.

After reflection I am wondering if the power supply is the issue. The test board that I have put together is running off 3 X 1.5V AA batteries giving an indicated 4.6 V at the pins without sensors attached - probably pulls down a little further under load. If the IRSeeker needs a full 5V for operation and the Compass is a bit more forgiving it may explain the symptoms I have been seeing. I will connect the board up to a regulated 5V supply tomorrow and see what happens.

Sorry for not having more info in the previous post - it was directed more towards my previous contributors to bring them up to speed on my progress.

I will post my findings after my next "play"
 

Technical

Technical Support
Staff member
We bought an irseeker to add to our NXT sensor collection on Friday so when it turns up next week we'll give it a go.
 

freddy2

New Member
Woah - scary stuff Technical! We only want to use the Hitechnics sensors on the PICaxe at this stage. I do like the idea of running a PICaxe for subsidiary I/O processing on a NXT system though. WIll have to research that further.
 

NXTreme

Senior Member
After reflection I am wondering if the power supply is the issue. The test board that I have put together is running off 3 X 1.5V AA batteries giving an indicated 4.6 V at the pins without sensors attached - probably pulls down a little further under load. If the IRSeeker needs a full 5V for operation and the Compass is a bit more forgiving it may explain the symptoms I have been seeing. I will connect the board up to a regulated 5V supply tomorrow and see what happens.
I have a copy of the book "Extreme NXT" that explains the basics of creating your own sensors for the NXT. There is a graph in the book (page 127 of second edition) that shows the amount of current the NXT can put out VS the voltage. The graph shows that the voltage varies from about 4.7V at no load to about 4V with 180 mA load, the maximum you can safely pull from the NXT. So, I believe that you could run the IRSeeker down to at least 4.2V no problem. YMMV.
 

mattallen37

New Member
Yes, we have used a 20X2 with a NXT numerous times, and have done numerous experiments with using both a 20X2 as a simulated slave (e.g. replacing an ultrasonic sensor with the PICAXE on an NXT brick) and using a 20X2 as master (ie NXT brick replacement) with numerous Lego/hitechnic sensors. You will see a number of tested NXT example files in the /samples folder of the Programming Editor software. There are also lots of schools doing this type of thing with robocup type robots, merging PICAXE chips in with Lego sensors / NXT bricks.

In each case it worked fine without any hacking.

What we wanted to stress here is
a) no NXT motherboard hacking is required at all
b) the recommended Lego external pullup resistor (inside sensor) of 82k works fine in all PICAXE scenarios
c) When using a Lego i2c sensor with a 20X2 master (e.g ultrasonic, i2c temperature or hitechnic color) there is no need to add pullups to the PICAXE circuit, as the pullups in the sensor work fine
d) When faking a NXT sensor (ie using the PICAXE as a slave to simulate the ultrasonic sensor) it works fine by adding 82k pullup resistors to the PICAXE system.

We see no reason to move away from what the Lego NXT recommendations are, they work! The circuitry inside the NXT is complex, linking the inputs to numerous non-standard i2c circuits and so where possible we recommend you just stick to what is known to work.

We have not opened the NXT as there has been no need, but even if the pullups are not fitted what is very clear is that there are a lot of elements on the i2c bus which are non standard such as diodes, series 4k7 resistor and pullup/down resistors (- e.g. having two diodes, 4k7 series resistor and 1M pull down resistor on an i2c line is extremely unusual). However in all cases the recommended circuit (82k pullup in the sensor) worked first time in our PICAXE experiments.
Please reread my post. I didn't say ANY motherboard hacking was required to get the 20X2 and NXT to work together. I was referring to a case when it was required, but not for a PICAXE, it was an IMU from SFE.

I know that 82k pullups work, but so do many other values (including the built in PICAXE pullups).
 

Technical

Technical Support
Staff member
We today tried an irseeker2 and it didn't work either, just giving 255s.
Then we remembered that the Lego ultrasonic requires both a 9V and a 5V supply, so it was worth a try - we hooked up the 9V extra supply and bingo, everything sprung to life!

So the irseeker2 needs 9V on pin 1 to operate.

We also got our IR ball out of the cupboard and realised the 1200/600HZ options are actually obvious when you have a ball - the ball has a switch on it for four different emit options - three of these are for use with the irseeker2 sensor

hi2cout [$10],$41,($00) ; AC mode (1200 Hz) - RCJ-05 Ball switch pos. D
;hi2cout [$10],$41,($01) ; AC mode (600 Hz) - RCJ-05 Ball switch pos. C
;hi2cout [$10],$41,($02) ; DC mode - RCJ-05 Ball switch pos. B

The photo shows our setup on the AXE216 i2c explorer board, which has been specifically designed for use with NXT sensors and building onto Lego. It'll be available soon.

Here is the working test code for compass and irseeker2 at the same time. Hope it helps! Remember to keep the compass level, that had us confused for a moment!

Code:
#picaxe 20x2
#no_table
' PICAXE-20X2 sample program for two Hitechnic NXT Sensors
' **** AXE216 i2c Experimenter Board Setup ****
' J1 External Power Fitted (4.5V)
' J2 SCL Pullup  Not fitted (pullup is in sensor)
' J3 SDA Pullup  Not fitted (pullup is in sensor)
' H2 9V Power  Fitted (9V)
 
' **** Hitechnic NXT Compass Sensor (part MS1034) ****
' **** Remember Compasss MUST be kept level ****
symbol heading_2degree = b0
symbol heading_adder = b1
symbol heading = w1 
 
' **** Hitechnic NXT IRSeeker V2 (part NSK1042) ****
' **** Remember IRSeeker V2 requires 9V power on wire 1 ****
' **** Tested using a Hitechnic robocup ball, version RCJ-05) ****
symbol ac_direction = b10
symbol sensor1_ac = b11
symbol sensor2_ac = b12
symbol sensor3_ac = b13
symbol sensor4_ac = b14
symbol sensor5_ac = b15
' Wires on NXT jack plug.
' Wire colours may vary. Pin 1 is always end nearest latch.
' 1 White +9V
' 2 Black 0V
' 3 Red 0V
' 4 Green V+ (4.5V or 5V)
' 5 Yellow SCL
' 6 Blue SDA
' Do not use i2c pullup resistor - already provided within sensor.
init:
 'Slave address of compasss is $02
 'Slave address of IRSeeker2 is $10
 hi2csetup i2cmaster, $02, i2cslow, i2cbyte
 'Optional configuration of compass
 hi2cout [$02],$41,($00)  ; normal mode
 ;hi2cout [$02],$41,($43) ; hard iron calibrate mode
 pause 100
 
 'Optional configuration of IRSEEKER2
  hi2cout [$10],$41,($00)  ; AC mode (1200 Hz)  RCJ-05 Ball switch pos. D 
  ;hi2cout [$10],$41,($01) ; AC mode (600 Hz)  RCJ-05 Ball switch pos. C
  ;hi2cout [$10],$41,($02) ; DC mode     RCJ-05 Ball switch pos. B
  pause 100
main:
 ; read values from compass (address $02)
 hi2cin [$02], $42,(heading_2degree, heading_adder) 
 
 heading = heading_2degree * 2 + heading_adder
 
 ; read values from IRSeeker  (address $10)
 hi2cin [$10], $49,(ac_direction,  sensor1_ac, sensor2_ac, sensor3_ac, sensor4_ac, sensor5_ac) 
 
 ; debug display
 debug
 ; wait 1 second then loop
 pause 1000
 goto main
 

Attachments

Technical

Technical Support
Staff member
We've also now managed to get the ultrasonic sensor working. It has faulty bit-busted i2c firmware, so requires an extra SCL pulse before the read.

Thanks to the Lejos firmware for finding that:
// Note: It appears that the Lego ultrasonic sensor needs a
// stop and an extra clock before the restart.

Here is working ultrasonic sensor code
Code:
#picaxe 20x2
#no_table
' PICAXE-20X2 sample program for Lego NXT Sensors
' **** AXE216 i2c Experimenter Board Setup ****
' J1 External Power Fitted (4.5V)
' J2 SCL Pullup  Not fitted (pullup is in sensor)
' J3 SDA Pullup  Not fitted (pullup is in sensor)
' H2 9V Power  Fitted (9V)
' Lego Ultrasonic Sensor (part 9846)
' Note this sensor is much more difficult than the other
' Lego sensors to use. Consider using the SRF005 instead!
' You must provide both 9V and 5V supplies .
' Also i2c needs to be hacked due to a bug in the ultrasonic i2c firmware
' This means only the ultrasonic sensor can be used at once on the i2c bus
' as we are now using a non-standard i2c protocol
' We recommnd using the SRF005 instead!
 
symbol distance = b0 'distance in cm
' Wires on NXT jack plug.
' Wire colours may vary. Pin 1 is always end nearest latch.
' 1 White +9V
' 2 Black 0V
' 3 Red 0V
' 4 Green +5V
' 5 Yellow SCL
' 6 Blue SDA
' Do not use i2c pullup resistor - already provided within sensor.
 
init:
 
 'Slave address is $02
 hi2csetup i2cmaster, $02, i2cslow, i2cbyte
 hi2cout $41,($02)  'continuous mode
 pause 100
main:
 
 ; read values
 ; This single line should work:
 ;hi2cin $42,(distance)
 ; but does not as dodgy firmware in ultrasonic sensor
 
 ; therefore here is the workaround
 
 hi2cout ($42)  ; address $42
 low B.7   ; fake an extra pulse
 input B.7   ; back to input
 hi2cin  (distance) ; read distance
 
 
 ; debug display
 debug
 ; wait 1 second then loop
 pause 1000
 goto main
 

papaof2

Senior Member
Remember to keep the compass level, that had us confused for a moment!
If you need an i2c compass for use where it will not be kept level, there are tilt-compensated devices. Sparkfun has one for $150US (+ shipping):
http://www.sparkfun.com/products/8656

Quick spec list: three axis of magneto, three axis of accelerometer, and a PIC core running all the calculations, giving accuracy of 2 degrees at level, 3 degrees at 15 degree tilt, 4 degrees at 60 degree tilt.

Or you could buy the two-axis i2c device from Sure Electronics ($8US, shipped), work out a look up table for corrections, and dedicate a 20X2 or 28X2 to the project. It would be somewhat larger than the Honywell device from Sparkfun ;-)

John
 

freddy2

New Member
Thanks Technical - that is brilliant !!! It is all obvious now (of course)

We tried the regulated 5V yesterday with no luck and I can't wait to get to work today and hook up a 9V supply.

I like the look of the AXE216 - can I get some further info?

Thanks also John for the I2C compass info.

The soccer field is flat so angle of sensor has nor been an issue. The bigger problem has been magnetic interference from the drive and dribbler motors. The dribbler is mounted on a hinged arm so it is difficult to calibrate [note that there are calibration routines that work fine for the fixed drive motors]. In the end we mounted the compass as high and as far away from the dribbler motor as the competition rules allowed and all worked well.

Thanks to everyone for their help. Despite using PICaxe processors in my school projects for 5 years this has been my first serious use of the forum and I have been amazed by the generosity, depth of knowledge and speed of responses. It has saved me weeks of work.
 

mattallen37

New Member
So, the ultrasonic sensor just needs an extra clock pulse (low) between the address, and the register (and obviously data byte returned)?

I haven't used the 20X2 as a master more than once, so I just want to understand it a little more.
 

Technical

Technical Support
Staff member
So, the ultrasonic sensor just needs an extra clock pulse (low) between the address, and the register (and obviously data byte returned)?
Yes, a number of people (all using different micros so not one system specific) have found that the Lego ultrasonic requires an extra, non-standard, pulse before the byte is read (all pulses under i2c are pulling the line low). The 'low : input' in our sample code does this.

What is very interesting is that the NXT brick must therefore also be giving out this dodgy pulse, or else the ultrasonic would not work. It is quite surprising therefore that all other i2c hitechnic style devices connected to the NXT to 'simulate an ultrasonic' seem to be tolerant of this extra pulse and completely ignore it.

However it is quite possible that a third party device, particularly if it has firmware bit-busted i2c, would get confused by this non-standard extra pulse and hence not work as expected with the NXT brick.

So in summary the NXT brick does not have standard i2c hardware or standard i2c firmware, but does still seem to work most of the time!
 
Last edited:

freddy2

New Member
Just for sake of completeness her is the demo program that I am showing the students. I have added a AXE033 display in I2C mode and (after a few false starts and lots of manual and forum post reading) ..... bob's yer uncle.

Thanks to all that assisted

Code:
#picaxe 28x1
#no_table
' PICAXE-28X1 sample program for two Hitechnic NXT Sensors plus AXE033
' display in I2C mode.
' Note : As operating on 4.5V supply installed jumpers on J1 and J2
' Hitechnic NXT Compass Sensor (part MS1034)
symbol heading_2degree = b0
symbol heading_adder = b1
symbol heading = w1 

 
' Hitechnic NXT IRSEeker V2
symbol ac_direction = b10
symbol sensor1_ac = b11
symbol sensor2_ac = b12
symbol sensor3_ac = b13
symbol sensor4_ac = b14
symbol sensor5_ac = b15
 
' Wires on NXT jack plug.
' Wire colours may vary. Pin 1 is always end nearest latch.
' 1 White 9V (for IRseeker)
' 2 Black 0V
' 3 Red 0V
' 4 Green V+ (4.5V or 5V)
' 5 Yellow SCL
' 6 Blue SDA
' Do not use i2c pullup resistor - already provided within sensor.
 
init:
 'Slave address of compasss is $02
 'Slave address of IRSeeker2 is $10
 'Slave address of AXE033 is $C6
 hi2csetup i2cmaster, $02, i2cslow, i2cbyte
 hi2csetup i2cmaster, $10, i2cslow, i2cbyte
 hi2csetup i2cmaster, $C6,i2cslow,i2cbyte
 'Optional configuration of compass
 hi2cout [$02],$41,($00)  ; normal mode
 ;hi2cout [$02],$41,($43) ; hard iron calibrate mode
 
 pause 500 
 
 'Optional configuration of IRSEEKER2
 hi2cout [$10],$41,($00)  ; normal AC mode
 ;hi2cout [$10],$41,($02) ; uncalibrated DC mode
 hi2cout [$c6], 0,(254,1,255) ' clearing screen
 pause 100
 hi2cout [$c6], 0,(254,128,"  SHC Soccerbot ",255) ' just some random message
 pause 100
 hi2cout [$c6], 0,(254,192," ..booting up.. ",255)' another random message
 pause 5000
 

 hi2cout [$c6], 0,(254,1,255)' clearing screen
main:
 ; read values from compass (address $02)
 hi2cin [$02], $42,(heading_2degree, heading_adder) 
 
 heading = heading_2degree * 2 + heading_adder
 bintoascii heading,b4,b5,b6,b7,b8'converting heading to ascii
 
 ; read values from IRSeeker  (address $10)
 hi2cin [$10], $49,(ac_direction,  sensor1_ac, sensor2_ac, sensor3_ac, sensor4_ac, sensor5_ac) 
 bintoascii ac_direction,b16,b17,b18 'converting direction to ascii
 hi2cout [$c6], 0,(254,128,"Compass    ",b6,b7,b8,255) 'displaying heading (0-360)
 pause 100
 hi2cout [$c6], 0,(254,192,"Direction    ",b18,255) 'displaying direction of ball (0-9)
 
 
 ; wait 1/10 second then loop
 
 pause 100
 goto main
 
Top