0

For a small project, I aim to connect a digital temperature sensor (KY-028), a Bluetooth module (HC-05) and a servomotor (SG90). The problem is it seems I can't connect the Bluetooth module and the servomotor together at the same time. The Bluetooth module together with the temperature sensor works fine. The servo with the sensor, again, all fine. But it seems these two don't agree with each other: as soon as I upload the code on the board (Arduino UNO), the servo starts vibrating (not even rotating), altough I made sure no command that asks it to do so is reached. Also while doing this, the Bluetooth module's led is turned off and it is unrecognizable by other devices.

When I upload the program to the board, I disconnect the RX and TX pins of the Bluetooth module from the pins 0 & 1 of the Arduino board, (otherwise I get errors and the program is not uploaded - if needed, I will get the error codes I get and edit the post), but after upload, the servo starts behaving weird, as described before. As I read on another article (if I manage to find the link again I will post it here), it might behave like this because there are unconnected pins in my whole circuit (which is true, but otherwise I can't connect the Bluetooth module). How could I possibly connect both of them on the same board?

I tested the code segments, they act as they should, I tried switching the pins so that I did not miss anything by mistake, as I am a total beginner (for instance I tried putting the signal wire of the servo on both PWM and not PWM supporting pins, tried connecting the Bluetooth module and the servo to different grounds etc.) but no success.

Any help would be much appreciated. If the layout of my circuit is needed, I will try to make one in a simulator. Thank you very much!

Edit

Here is the code:

#include <Servo.h>
#include <SoftwareSerial.h>

int txPin = 1;
int rxPin = 0;
int digitalPin = 7; // KY-028 digital interface
int analogPin = A2; // KY-028 analog interface
int angle = 0;
int temp;
bool incTemp = false, decTemp = false;

Servo servo;
SoftwareSerial BT(txPin,rxPin);

void setup() {
  pinMode(digitalPin, INPUT);
  pinMode(analogPin, INPUT);
  
  servo.attach(11);
  delay(100);

  BT.begin(9600);
  Serial.begin(9600);
}

void loop() {
  temp = analogRead(analogPin);
  Serial.println(temp); 

  if (BT.available())
  {
    char bytesToSend[2];
    bytesToSend[0] = 101;
    bytesToSend[1] = temp;
    bytesToSend[2] = 110;
    BT.println(bytesToSend);
  }

  if(incTemp)
  {
    servo.write(90);
    delay(100);
    servo.write(135);
    delay(100);
    servo.write(180);
    delay(100);
    servo.write(135);
    delay(100);
  }
  else if(decTemp)
  {
    servo.write(90);
    delay(100);
    servo.write(45);
    delay(100);
    servo.write(0);
    delay(100);
    servo.write(45);
    delay(100);
  }

  delay(2000);
}

For the incTemp and decTemp variables I will use an interrupt to detect when the Bluetooth paired device is sending a command to activate the servomotor. But for now, as it can be seen, the blocks that ask the servo to rotate cannot be reached. For the layout I will try to find a simulator that provides the components I use and I will come back with the design.

Later edit, after following Majenko's advice

Here is the currently working code. An answer with the full implementation will come when the project is complete.

#include <Servo.h>

int txPin = 1;
int rxPin = 0;
int digitalPin = 7; // KY-028 digital interface
int analogPin = A2; // KY-028 analog interface
int angle = 0;
int temp;
bool incTemp = false, decTemp = false;

Servo servo;

void setup() {
  pinMode(digitalPin, INPUT);
  pinMode(analogPin, INPUT);

  Serial.begin(9600);
}

void loop() {
  temp = analogRead(analogPin);
  Serial.print("T");
  Serial.print(temp); 
  Serial.println();

  if(incTemp)
  {
    servo.attach(11);
    delay(100);
    
    servo.write(90);
    delay(100);
    servo.write(135);
    delay(100);
    servo.write(180);
    delay(100);
    servo.write(135);
    delay(100);

    servo.detach();
    delay(100);
  }
  else if(decTemp)
  {
    servo.attach(11);
    delay(100);
    
    servo.write(90);
    delay(100);
    servo.write(45);
    delay(100);
    servo.write(0);
    delay(100);
    servo.write(45);
    delay(100);

    servo.detach();
    delay(100);
  }

  delay(2000);
}
6
  • 3
    Please show your code and your wiring.
    – Majenko
    Dec 14 '20 at 23:29
  • @Majenko I provided the code. But for the simulator I am not quite sure what to use. Neither Tinkercad nor Fritzing provide all the components I use... Dec 15 '20 at 14:35
  • Maybe this: arduino.stackexchange.com/questions/57584/…
    – ocrdu
    Dec 15 '20 at 14:55
  • 3
    I had a hunch I might see this: SoftwareSerial BT(txPin,rxPin); ... NEVER use SoftwareSerial on pins 0/1 as they are the HARDWARE UART. You are using the right pins, but DO NOT use SoftwareSerial. Use Serial.
    – Majenko
    Dec 15 '20 at 14:56
  • 1
    The Serial sends data over pins 0 and 1. Those pins are also connected directly to the USB interface chip. Which is also why you have to disconnect bluetooth to upload your code.
    – Majenko
    Dec 15 '20 at 23:06
1

So, I got carried away with my exams and I forgot to come back with an update and a solution. To take it short, I added a new source of power to the circuit and I am only turning the servo on when needed to rotate, then turn it off again. The code can be seen below:

#include <Servo.h>
#include <SoftwareSerial.h>

int rxPin = 2;
int txPin = 3;
int digitalPin = 7; // KY-028 digital interface
int analogPin = A2; // KY-028 analog interface
int servoPin = 11;

int angle = 0;
int temp;

const int minTemp = 150;
const int maxTemp = 350;
const int minAngle = 0;
const int maxAngle = 180;
const char sendDataPackageStartByte = 'T';
const int receiveDataPackageStartByteVal = 83; //"S"
const int receiveDataPackageEndByteVal = 13; 
const int communicationDelay = 1000;
const int servoDelay = 100;

SoftwareSerial BT(rxPin,txPin);

Servo servo;
void setup() {
  pinMode(digitalPin, INPUT);
  pinMode(analogPin, INPUT);

  Serial.begin(9600);
  BT.begin(9600);
}

void loop() {
  if(BT.available() <= 0)
  {
    temp = analogRead(analogPin);
    BT.print(sendDataPackageStartByte);
    BT.print(temp/100);
    BT.print(temp/10%10);
    BT.print(temp%10); 
    BT.println();
    delay(communicationDelay);
  }
  else 
  {
    readData();
  }
}

void readData()
{
  if (BT.available())
  {       
    char temp[3];
    while(BT.available() > 0)
    {
      char crtByte = BT.read();
      if (crtByte == receiveDataPackageStartByteVal) 
      {
        temp[0] = BT.read();
        temp[1] = BT.read();
        temp[2] = BT.read();

        if(BT.read() != receiveDataPackageEndByteVal)
        {
          return;
        }
    
        break;
      } 
    }

    int desiredTemp = atoi(temp);
    int desiredAngle = map(desiredTemp, minTemp, maxTemp, minAngle, maxAngle);
    printDataToSerial(desiredTemp, desiredAngle);   

    turnServo(desiredAngle); //attach servo, write value, dettach
  }
}

void printDataToSerial(int desiredTemp, int desiredAngle)
{
  Serial.println("Desired Temperature:");
  Serial.println(desiredTemp);

  Serial.println("Desired Angle:");
  Serial.println(desiredAngle);
}

void turnServo(int angle)
{
  servo.attach(servoPin);
  delay(servoDelay);
  servo.write(angle);
  delay(servoDelay);
  servo.detach();
  delay(servoDelay);
}

Also, here would be the layout of my final circuit:

circuit

Basically what I wanted to achieve was sending and displaying the current temperature from an Arduino device to an Android one, then allow the user to adjust the temperature, simulating this with a servo commanded over Bluetooth. For those interested in the Android part as well (and the whole project actually), here is the link to the project repository on GitHub. The Android part is written with C# in the Xamarin Android framework. I hope this helps anyone landing here, good luck! :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.