Code Examples
Code Examples
Code Examples
Code Examples
(218) 454-0766
[email protected] MAXBOTIX (1)
Code example for the Basic Micro, Atom
‘BasicAtom Code
‘Reads MaxSonar®-EZ1™
‘Bob Gross
’01/14/2005
‘5V connect to +5V
‘GND connect to common
‘TX, connect to Atom P7
‘RX, connect to Atom P5
‘The serial data will be sent when the reading is complete,
‘This is very fast when an object is close.
‘Only four lines of code required
(218) 454-0766
[email protected] MAXBOTIX (2)
Code example using DevBoard-M32 (AVR using Bascom)
‘Using the MaxSonar®-EZ1™ with the DevBoard-M32
‘By Eddy Wright, Wright Hobbies Robotics, 2006
‘http://www.wrighthobbies.net
‘We will read both the analog and serial outputs of the MaxSonar®
‘This code can be used with any AVR with ADC that is supported by Bascom
‘Config the softare UART, we need to use the INVERTED option with the MaxSonar®
Open “comd.7:9600,8,n,1,INVERTED” For Input As #1
‘Configure ADC
Config Adc = Single , Prescaler = Auto , Reference = Internal
Start Adc
Do
Dist = Getadc(0)
Shift Dist , Right , 2 ‘The M32 has 10bit ADC, shifting it twice makes it 8bit
Input #1 , Strdist Strdist = Right(strdist , 3) ‘Strip off the letter R
Serdist = Val(strdist) ‘Convert to a number
Print “Analog Distance = ” ; Dist ; ” Inches”
Print “Serial Distance = ” ; Serdist ; ” Inches”
Loop
(218) 454-0766
[email protected] MAXBOTIX (3)
Code Example using Parallax Basic Stamp BS2, BasicStamp
(218) 454-0766
[email protected] MAXBOTIX (4)
C Program Driver for MaxBotix MB7066 PW Ultrasonic Sensor
By Paul Sfetku, July 2011
Development System – Mikroelektronika
http://www.mikroe.com/
Develop. Board: BigPic6 with GLCD (128×64)
Microcontroller: PIC18F8520/8722 family
Compiler: mikroC PRO for PIC
Interface: BigPic6 SW7 RG0: PORTG.B0 (output) → MB7066<4> start/stop ranging
BigPic6 SW7 RG1: PORTG.B1 (input) → MB7066<2> PW – pulse width
NOTE: BigPic6 SW7 is set for pull-up on PORTG pins RG0/B0 & RG1/B1.
The MB7066 is operated in free running mode with MB7066<4> pulled High. To operate the MB7066 in start/stop
mode, you can uncomment code statements 8,9,10 and code statements 17,18,19.
Statements 15-20 can be used to determine when and if the MB7066 fails to detect a return pulse from the given
target. A value for maxvalue in statement 15 is needed to calibrate the sensor for a maximum time or maximum
distance value.
The code to drive the MB7066 is contained within an infinite loop declared in statement 11. The MB7066 operates on
a 100ms PW cycle. MB7066 pin-2 PW sets high to start a ranging cycle, and sets Low if and when a target object is
detected. If no target is detected, pin-2 PW will be held high for up to 62ms or 1068cm, the maximum range value.
Code statements 12-25 are designed detect and measure the period of each PW begin/end cycle. Statement 12
detects when the PW goes High. The delay in statement 13 is used to calibrate the sensor’s ranging resolution to
detect changes at one inch increments. The variable in statement 14 counts the number of delays until the PW signal
does low.
Statements 21-24 calculate the range and detect the beginning of the next 100ms PW cycle. Statement 22 calculates
the range based on the time counted in statement 14, and based on a constant factor (.5) that can be adjusted to
provide an accurate range measurement.
(218) 454-0766
[email protected] MAXBOTIX (5)
C Program Driver for MaxBotix MB7066 PW Ultrasonic Sensor Cont.
The loop in statement 24 is used to wait for the PW to set High, indicating the beginning of the next 100ms PW
ranging cycle.
// Declarations ——————————————————-
TRISG = 0x02; // PORTG DDR: RG0 (output), RG1 (input)
unsigned int msec = 0; // time counter – microseconds
unsigned int inches = 0; // calculated distance
unsigned maxvalue = 0; // sensor maximum – echo not detected
void main() {
//PORTG.B0 = 0; // MB7066<4>: stop ranging – set Low
//PORTG.B0 = 1; // MB7066<4>: start ranging – set High
//Delay_us(20); // 20us delay for start of ranging
while (1) { // infinite loop
if (PORTG.B1) { // MB7066<2>: PW – check if High
Delay_us(30); // delay factor 30us – gives 1 inch resolution
msec++; // count time PW is High each microseconds
//if (msec > maxvalue) { // PW max time 62ms -> echo not detected
// msec = 0; // clear counter
// PORTG.B0 = 0; // MB7066<4>: stop ranging – set Low
// PORTG.B0 = 1; // MB7066<4>: start ranging – set High
// Delay_us(20); // 20us delay for start of ranging
//}
} else { // MB7066<2>: PW -> Low
inches = .5*msec; // range (inches): target correction factor = .5
msec = 0; // clear counter
while (!PORTG.B1); // wait for MB7066<2> PW to go High -> start 100ms range cycle
}
}
}
(218) 454-0766
[email protected] MAXBOTIX (6)
Arduino I2C Code Example for I2CXL-MaxSonar Products
Development System – Arduino Uno (as of Arduino 1.0.1)
Develop. Board: Arduino Uno R3
void setup() {
Serial.begin(9600);//Open serial connection at 9600 baud
Wire.begin();//Initiate Wire library for I2C communications with I2CXL‑MaxSonar‑EZ
}
void loop() {
takeRangeReading(); //Tell the sensor to perform a ranging cycle
delay(100); //Wait for the sensor to finish
word range = requestRange(); //Get the range from the sensor
Serial.print(“Range:”);Serial.println(range); //Print to the user
}
(218) 454-0766
[email protected] MAXBOTIX (7)
Arduino I2C Code Example for I2CXL-MaxSonar Products Cont.
//Returns the last range that the sensor determined in its last ranging cycle in centimeters. Returns 0 if there is no
communication.
word requestRange(){
Wire.requestFrom(SensorAddress, byte(2));
if(Wire.available() >= 2){ //Sensor responded with the two bytes
byte HighByte = Wire.read(); //Read the high byte back
byte LowByte = Wire.read(); //Read the low byte back
word range = word(HighByte, LowByte); //Make a 16-bit word out of the two bytes for the range
return range;
}
else {
return word(0); //Else nothing was received, return 0
}
}
byte temp;
if(SevenBitHuh){ temp = newAddress << 1; } //The new address must be written to the sensor
else { temp = newAddress; } //in the 8bit form, so this handles automatic shifting
Wire.write(temp); //Send the new address to change to
Wire.endTransmission();
}
(218) 454-0766
[email protected] MAXBOTIX (8)