TFMini-S not working with Arduino Mega

Hello Everyone,
I have project where i am using Arduino MEGA 2560 and TFMini-S for distance measurement.
I uploaded the pre-made library example code as shown below with pin 53 & 51 for TX & RX using Arduino MEGA.

Currently i am getting the message in Serial communication monitor as-
TF Mini error: too many measurement attempts
Last error:
65535 cm sigstr: 65535

Can anyone please help me out as i have very limited information for MEGA and how RX/TX works ?
Thank you in advance. Look forward to hear any positive news.

The code i am using-

#include <SoftwareSerial.h>
#include "TFMini.h"

// Setup software serial port
SoftwareSerial mySerial(53, 51); // Uno RX (TFMINI TX), Uno TX (TFMINI RX)
TFMini tfmini;

void setup() {
// Step 1: Initialize hardware serial port (serial debug port)
// wait for serial port to connect. Needed for native USB port only
while (!Serial);

Serial.println ("Initializing...");

// Step 2: Initialize the data rate for the SoftwareSerial port

// Step 3: Initialize the TF Mini sensor

void loop() {
// Take one TF Mini distance measurement
uint16_t dist = tfmini.getDistance();
uint16_t strength = tfmini.getRecentSignalStrength();

// Display the measurement
Serial.print(" cm sigstr: ");

// Wait some short time before taking the next measurement

Why are you using software serial on the Mega when it has three more hardware serial ports on the board?

What do you want to do with this data?

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

Hey Mike, I simply copy the MOSI & SS pins from UNO and used in MEGA.

Besides i have more devices already using PWM pins. And 3 of TFMini-S already hooked up. Code that i posted here is just for setup 1 TFMini to make sure it works OK. Than COPY logic all over in project.

Thanks Stefan,
I will do that next time.

That is where you are going wrong. Why not use one of those free serial ports? Software serial is a last resort when it comes to communicating.

All those pins on the Mega are first and foremost digital pins, then they have other uses if you want them.

Hi, @Riski6

// Setup software serial port
SoftwareSerial mySerial(53, 51); // Uno RX (TFMINI TX), Uno TX (TFMINI RX)
TFMini tfmini;

Where does the UNO come in?

Tom... :smiley: :+1: :coffee: :australia:

Hi Mike,
I could use them if i choose not to go with some RGB LEDs with PWMs.
I have few PWM bits already used by motors and LEDs.

So, i selected those 51,53 bits for RX&TX while trying to test my code shown above before dumping in my main code. Which is not complete yet. I watched several videos to understand all the hardware 1st and code as i go. I will see if i can switch few bits around to use TFMini.

Thanks for the replys.

Hi Tom,
The code you see is standard code from the example that comes with TFMini-S Library. This code is only designed with UNO that's why you see UNO !
But my actual hardware has more PWM and motors, ultimately made me to use MEGA.
I will go with what mike said and re-arrange the pinout or may be take some LED out to fit everything in.

Thanks for the reply.

Do you know there is a soft PWM library?
Much better to use this than a soft serial library.


Have you tried the example code on a UNO, to prove it works for a start?

Tom... :smiley: :+1: :australia:

This is happening with my system too. I tried the given solution, let's see!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.