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);
}
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. UseSerial
.