Show Posts
Pages: [1]
1  Using Arduino / Networking, Protocols, and Devices / Single Wire Hardware Serial on: March 04, 2011, 03:35:58 pm
This is a continuation of my thread from here:,22664.msg171332.html

I tried connecting with the circuit in that thread, 220ohm resistors in the TX and RX lines before connecting together. I could not communicate at all with the ultrasonic sensor. (Deleted the code inadvertently so I can't post it). I also tried using NewSoftSerial. While the sensor indicated communication, the data I got back was bogus.

Here is the code I used using SoftwareSerial, which works fine (modified from an example program provided with sensor). It gets the software version from the sensor in the setup, then loops over and over, doing 100 ranges and averaging them. If the distance is less than 10, onboard LED is illuminated.

#include <SoftwareSerial.h>

#define txrxPin 10                                          // Defines a pin to be used as both rx and tx for the SRF01
#define srfAddress 0x01                                    // Address of the SFR01
#define getSoft 0x5D                                       // Byte to tell SRF01 we wish to read software version
#define getRange 0x53                                      // Byte used to get range from SRF01 in inches
#define getStatus 0x5F                                     // Byte used to get the status of the transducer

SoftwareSerial UltrasonicBus(txrxPin, txrxPin);

void setup(){
  delay(200);                                              // Waits some time to make sure that SRF01 is powered up
  Serial.println("SRF01 Test");                           
  SRF01_Cmd(srfAddress, getSoft);                          // Calls a function to get the SRF01 software version
  int softVer =;                             // Read software version from SRF01
  Serial.println(softVer);                                   // Prints the software version to LCD03
  Serial.println("Initialization Complete");

void loop(){
  int max = 10;
  int sum = 0;
  for (int count = 0; count < max; count++) {
    sum = sum + doRange();
  int range = sum / max;
  Serial.print("Range 100avg = ");                               
  Serial.print(range);                                     // Print range result to the screen
  Serial.println("  ");                                      // Print some spaces to the screen to make sure space direcly after the result is clear
    digitalWrite(13, HIGH);
  } else {
    digitalWrite(13, LOW);

void SRF01_Cmd(byte Address, byte cmd){                     // Function to send commands to the SRF01
  pinMode(txrxPin, OUTPUT);                                 // Set pin to output and send break by sending pin low, waiting 2ms and sending it high again for 1ms
  digitalWrite(txrxPin, LOW);                             
  digitalWrite(txrxPin, HIGH);                           
  UltrasonicBus.print(Address, BYTE);                              // Send the address of the SRF01
  UltrasonicBus.print(cmd, BYTE);                                  // Send commnd byte to SRF01
  pinMode(txrxPin, INPUT);                                  // make input ready for Rx

void checkLock() {
  SRF01_Cmd(srfAddress, getStatus);                         // Call to a function that checks if the trancducer is locked or unlocked
  byte statusByte =;                          // Reads the SRF01 status, The least significant bit tells us if it is locked or unlocked
  int status = statusByte & 0x01;                           // Get status of lease significan bit
  if(status == 0){                                     
    Serial.println("Unlocked");                            // Prints the word unlocked followd by a couple of spaces to make sure space after has nothing in
   else {                                     
    Serial.println("Locked   ");                              // Prints the word locked followd by a couple of spaces to make sure that the space after has nothing in

int doRange() {
  SRF01_Cmd(srfAddress, getRange);                         // Calls a function to get range from SRF01
  byte highByte =;                           // Get high byte
  byte lowByte =;                            // Get low byte
  int dist = ((highByte<<8)+lowByte);                     // Put them together
  return dist;

I tried using NewSoftSerial with this same code, just changing the appropriate SoftwareSerial references to NewSoftSerial. The data light on the sensor flashed as normal to indicated communication, but the data I got back was bogus- the software version, which should return "1", returned "-1." The distance measurement numbers were all random. I was getting fluctuating numbers in the 500-700 range while I should have been getting steady numbers in the 5-10 range. I suspect they might have been even more all over the place but the averaging probably smoothed it.

I am suspecting it is a timing issue. My understanding is SoftwareSerial is polling based, which allows them to use delays as necessary in getting the attention of the transducer, but since NewSoftSerial and hardware serial are interrupt based, the delays do nothing..............?

Anyone have some suggestions to this? Code greatly appreciated if anyone can post some.

2  Using Arduino / Installation & Troubleshooting / Mega 2560- USB connection while using 5V external power on: March 04, 2011, 02:49:41 pm
How can I safely power the Arduino through the 5V pin and utilize the USB connection?

I've searched but haven't found a totally clear answer on this.

I have a project using a bunch of Sharp IR distance sensors and some ultrasonic range finders with a Mega 2560. Everything worked well initially but once I added all the sensors, I think we were overloading the USB port as the 5V rail was only giving around 4.25-4.5V. I used a benchtop 5V supply to power the sensors (grounds tied of course) and left the Arduino on USB. The IR sensors worked fine, but I found the ultrasonic sensors act up when powered by a separate power supply, returning bogus numbers (im using SRF01's). Disconnecting the USB and powering the Arduino through the 5V pin with the bench supply so everything was being fed by the same power supply, everything worked fine.

Problem is- I need the USB connection for debugging. So my question is again: How to safely power through the 5V pin AND use the USB connection, or is it even possible?
- Can I just cut the 5V line on the USB cable? Will the USB converter power off the 5V pin from the board or is the 5V USB connection its only power source?
- I have a FTDI breakout cable. I assume I could also just use that and not connect the power pin to the board. If I did that, what pins need to go where from the cable to the board?

3  Forum 2005-2010 (read only) / Interfacing / Re: Single Wire Hardware Serial, Possible? on: January 23, 2011, 09:46:24 pm
Awesome. Thanks for the tips. I will hook up drone's circuit this week and try it out. I plan on using one of the extra serial ports on the Mega so it won't interfere with uploading.
4  Forum 2005-2010 (read only) / Interfacing / Single Wire Hardware Serial, Possible? on: January 21, 2011, 11:45:15 am
I'm using an SRF01 ranger for a project with a Mega 2560.
This device uses a single wire for bi-directional serial communication. The example uses a software serial port with both the TX and RX pins defined as the same pin and just changes the pinMode depending on whether you are sending or receiving. My understanding is software serial is limited to 9600 baud and is very time intensive.

Is there any way to utilize one of the additional hardware serial ports on the mega for single wire communication?

5  Forum 2005-2010 (read only) / Interfacing / Replacing Relays with Arduino Controlled MOSFETs on: March 27, 2010, 09:07:20 am
I have a small project I'm working on. I have a bunch of accessories in my car- lights primarily. I would like to build an interactive control system for them using an Arduino and have the ability to dim the lights if I choose. My understanding is MOSFETs are the desired high current switching device of choice for lower voltages.

My two main questions are
1) As long as a MOSFET is switched hard on, there are essentially no losses, correct? So to drive a pair of 55W lights for instance, as long as I have a MOSFET rated for at least 10 amps, I should be ok and there will be no substantial heat problems if i'm not PWMing?

2) What will be the ideal way to get signal from the Arduino in the cabin to the MOSFETs in my box under the hood? I thought about using CAT5 for the interconnect, simply because it's cheap and has 8 conductors. Will using logic level MOSFETs with direct connection between them and the Arduino work, going about 5 feet? Or would it be better to use standard MOSFETs with the Arduino driving gate drivers and then making the long distance run at higher voltage? Will my CAT5 idea even work or am I going to need shielded cable?

Pages: [1]