I have a robot project and i am using rx tx pin for communicating with esp32 cam.I want to use also dffplayer but rx tx pins are full and digital pins are limited.So i want to use analog pins instead of rx tx pins for communicating with dffplayer
Hi,
Reassign some of your digital pins to the analog pins, as digital mode, then use the pure digital pins a software serial.
I'm not sure if analog pins can be used for software serial.
pinMode(A0, OUTPUT);
pinMode(A1,INPUT);
Puts them in digital mode.
Tom....
![]()
PS. Correct me if I'm wrong.
There is no mention in the reference that it should not do the job.
analog pins on an UNO can be used as digital pins, you can just keep counting
eg A0 = 14, A1 = 15 etc. which will give you a maximum of 19 digital pins (this includes rx & tx hwSerial)
All pins support Pin Change interrupts so it should work with swSerial, but you can consider moving things from the other digital pins.
Keep in mind that swSerial is not reliable at higher baud rates (for sure not for reception) 38400bps is a safe limit.
On a Nano & Pro-mini A6 & A7 can only be used as analog inputs,
Which Arduino board have you run out of pins on ?
arduino uno card
so i should change analog pins to digital pins and use the normal gpio pins like 12,13,14 as rx tx pins
okay i will use analog pins as digital pins and other digital pins like 3,4,5 as rx tx pins
as i said, you can also just use the analog pins, for tx/rx, but you should name them by their digital name.
You can see they have PCINT capability, so they work (or should do at least)
Any specific reason why you suggest that? Most beginners do not know that A0 translates to 14 on an Uno or 54 on a Mega. For me it's clearer to use the A numbers because the boards are marked with that.
First of all, simply because i was taught that, but thinking about it, more because i would recommend using HIGH & LOW, for digitalWrite() and not true & false. There is no guarantee that 'A0' actually translates to '14' , it does now, but that does not mean it will always be like it. The numbers for the digital pins are the numbers for the digital pins, the analog names are the reference for their connection to the ADC. That it works to reference a pin by it's analog name (it does i guess, i never do) on an UNO, does not mean it will work on all boards. Hence the picture of the pinout, which includes many more purposes for the pins and their capabilities.
Sorry but I don't follow your reasoning for using pin numbers for analogue pins.
You can refer to a pin as A0 and still use HIGH/LOW as its state and if is possible that the mapping between A0 and pin 14 might change at some time then it is surely better to refer to the pin as A0
That's a different point.
That's why you should use the A numbers in my opinion for analogue pins. They will always be consistent regardless if you use an Uno or a Mega or one of the more modern boards.
As long as the pin provides both digital and analogue functionality you can use digitalRead(Ax) and digitalWrite(Ax). That way code can easily be ported from Uno to e.g. Mega; with your approach you will basically need to modify the code when porting, when using the A numbers it's only a matter of recompile for the correct board.
Yes it is.
My point is that you should look at the pinout not at the PCB silkscreen print.
Again, one should look at the pinout diagram. Whatever is written on the board is just a direct visual reference. (which in case of knock-off clones may not even be correct or readable at all)
Between those 2 boards, maybe, but one should just refer to the correct numbers anyway.
Anyway, all in all a rather pointless discussion in my opinion.
I am sorry but I think that you are flogging a dead horse.
If you cannot believe the designations of the A* pins printed on the board then presumably neither can you believe the pin numbers themselves printed on the board
It is not about believe it is about what is the most reliable reference. I've have some boards (mega clones) with incorrect silkscreen print.
I named their digital names in the code but it didn't work.Also named analog pins in the code again didn't work
here is my code:
#include <Servo.h>
#include <DHT.h>
#include <DFRobotDFPlayerMini.h>
#include <SoftwareSerial.h>
#define DHT_PIN 13
#define TRIG_PIN_LEFT 2
#define ECHO_PIN_LEFT 3
#define TRIG_PIN_RIGHT 4
#define ECHO_PIN_RIGHT 5
#define SOL_MOTOR_IN1 6
#define SOL_MOTOR_IN2 7
#define SAG_MOTOR_IN3 8
#define SAG_MOTOR_IN4 9
#define SOL_MOTOR_ENA 14
#define SAG_MOTOR_ENB 15
#define VAKUM_MOTOR 16
#define SERVO_LEFT_PIN 17
#define SERVO_RIGHT_PIN 18
#define DFP_RX_PIN 10
#define DFP_TX_PIN 11
#define DHT_TYPE DHT11
DHT dhtSensor(DHT_PIN, DHT_TYPE);
Servo servoLeft;
Servo servoRight;
SoftwareSerial dfpSerial(DFP_TX_PIN, DFP_RX_PIN);
DFRobotDFPlayerMini MP3Player;
int motorHizi = 200;
bool otomatikMod = false;
bool manuelMod = false;
bool vakumDurumu = false;
bool muzikCaliyor = false;
int currentTrack = 1;
unsigned long sonSensorGuncelleme = 0;
const int SENSOR_UPDATE_INTERVAL = 1000;
int sesSeviyesi = 15;
int mesafeLeft = 0;
int mesafeRight = 0;
float sicaklik = 0;
float nem = 0;
const int MIN_MESAFE = 15;
unsigned long sonYonDegisimZamani = 0;
int servoLeftPos = 90;
int servoRightPos = 90;
void setup() {
Serial.begin(115200);
dfpSerial.begin(9600);
pinMode(TRIG_PIN_LEFT, OUTPUT);
pinMode(ECHO_PIN_LEFT, INPUT);
pinMode(TRIG_PIN_RIGHT, OUTPUT);
pinMode(ECHO_PIN_RIGHT, INPUT);
pinMode(SOL_MOTOR_IN1, OUTPUT);
pinMode(SOL_MOTOR_IN2, OUTPUT);
pinMode(SAG_MOTOR_IN3, OUTPUT);
pinMode(SAG_MOTOR_IN4, OUTPUT);
pinMode(SOL_MOTOR_ENA, OUTPUT);
pinMode(SAG_MOTOR_ENB, OUTPUT);
pinMode(VAKUM_MOTOR, OUTPUT);
servoLeft.attach(SERVO_LEFT_PIN);
servoRight.attach(SERVO_RIGHT_PIN);
servoLeft.write(90);
servoRight.write(90);
dhtSensor.begin();
motorDurdur();
digitalWrite(VAKUM_MOTOR, LOW);
if (!MP3Player.begin(dfpSerial)) {
Serial.println("DFPlayer Mini başlatılamadı!");
} else {
MP3Player.volume(sesSeviyesi);
MP3Player.EQ(DFPLAYER_EQ_NORMAL);
MP3Player.outputDevice(DFPLAYER_DEVICE_SD);
}
randomSeed(analogRead(A5));
Serial.println("ARDUINO_READY");
}
void loop() {
if (Serial.available()) {
String komut = Serial.readStringUntil('\n');
komut.trim();
islemKomut(komut);
}
if (millis() - sonSensorGuncelleme > SENSOR_UPDATE_INTERVAL) {
sensorVerileriniGuncelle();
sensorVerileriniGonder();
sonSensorGuncelleme = millis();
}
if (otomatikMod) {
otomatikModKontrol();
}
}
void islemKomut(String komut) {
if (komut == "OTOMATIK_AC") {
otomatikMod = true;
manuelMod = false;
}
else if (komut == "OTOMATIK_KAPAT") {
otomatikMod = false;
motorDurdur();
}
else if (komut == "MANUEL_AC") {
manuelMod = true;
otomatikMod = false;
}
else if (komut == "MANUEL_KAPAT") {
manuelMod = false;
motorDurdur();
}
else if (komut == "VAKUM_AC") {
vakumDurumu = true;
digitalWrite(VAKUM_MOTOR, HIGH);
Serial.println("VAKUM:ACIK");
}
else if (komut == "VAKUM_KAPAT") {
vakumDurumu = false;
digitalWrite(VAKUM_MOTOR, LOW);
Serial.println("VAKUM:KAPALI");
}
else if (komut == "YON:F") {
if (manuelMod) {
ileriGit();
}
}
else if (komut == "YON:B") {
if (manuelMod) {
geriGit();
}
}
else if (komut == "YON:L") {
if (manuelMod) {
solaGit();
}
}
else if (komut == "YON:R") {
if (manuelMod) {
sagaGit();
}
}
else if (komut == "YON:S") {
motorDurdur();
}
else if (komut.startsWith("HIZ:")) {
motorHizi = komut.substring(4).toInt();
motorHizi = constrain(motorHizi, 0, 255);
}
else if (komut == "MUZIK_PLAY") {
muzikCaliyor = true;
MP3Player.play(currentTrack);
Serial.println("MUZIK:CALIYOR");
}
else if (komut == "MUZIK_PAUSE") {
muzikCaliyor = false;
MP3Player.pause();
Serial.println("MUZIK:DURAKLATILDI");
}
else if (komut == "MUZIK_NEXT") {
currentTrack++;
if (currentTrack > 5) currentTrack = 1;
MP3Player.play(currentTrack);
Serial.println("MUZIK:TRACK:" + String(currentTrack));
muzikCaliyor = true;
}
else if (komut == "MUZIK_PREV") {
currentTrack--;
if (currentTrack < 1) currentTrack = 5;
MP3Player.play(currentTrack);
Serial.println("MUZIK:TRACK:" + String(currentTrack));
muzikCaliyor = true;
}
else if (komut.startsWith("MUZIK_VOLUME:")) {
sesSeviyesi = komut.substring(13).toInt();
sesSeviyesi = constrain(sesSeviyesi, 0, 30);
MP3Player.volume(sesSeviyesi);
}
else if (komut.startsWith("MUZIK_TRACK:")) {
currentTrack = komut.substring(12).toInt();
currentTrack = constrain(currentTrack, 1, 5);
MP3Player.play(currentTrack);
Serial.println("MUZIK:TRACK:" + String(currentTrack));
muzikCaliyor = true;
}
else if (komut == "TEST_KOMUT") {
Serial.println("ARDUINO_TEST_OK");
}
}
void sensorVerileriniGuncelle() {
mesafeLeft = ultrasonikMesafeOlc(TRIG_PIN_LEFT, ECHO_PIN_LEFT);
mesafeRight = ultrasonikMesafeOlc(TRIG_PIN_RIGHT, ECHO_PIN_RIGHT);
sicaklik = dhtSensor.readTemperature();
nem = dhtSensor.readHumidity();
if (isnan(sicaklik) || isnan(nem)) {
sicaklik = 25;
nem = 50;
}
}
void sensorVerileriniGonder() {
String jsonVeri = "{";
jsonVeri += "\"mesafe\":" + String(min(mesafeLeft, mesafeRight)) + ",";
jsonVeri += "\"sicaklik\":" + String(sicaklik) + ",";
jsonVeri += "\"nem\":" + String(nem) + ",";
jsonVeri += "\"gaz\":0,";
jsonVeri += "\"hareket\":0,";
jsonVeri += "\"ses\":0,";
jsonVeri += "\"vakum\":" + String(vakumDurumu ? "true" : "false") + ",";
jsonVeri += "\"otomatik\":" + String(otomatikMod ? "true" : "false") + ",";
jsonVeri += "\"manuel\":" + String(manuelMod ? "true" : "false") + ",";
jsonVeri += "\"muzik\":" + String(muzikCaliyor ? "true" : "false") + ",";
jsonVeri += "\"track\":" + String(currentTrack);
jsonVeri += "}";
Serial.println("SENSOR_DATA:" + jsonVeri);
}
int ultrasonikMesafeOlc(int trigPin, int echoPin) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
return duration * 0.034 / 2;
}
void ileriGit() {
digitalWrite(SOL_MOTOR_IN1, HIGH);
digitalWrite(SOL_MOTOR_IN2, LOW);
digitalWrite(SAG_MOTOR_IN3, HIGH);
digitalWrite(SAG_MOTOR_IN4, LOW);
analogWrite(SOL_MOTOR_ENA, motorHizi);
analogWrite(SAG_MOTOR_ENB, motorHizi);
}
void geriGit() {
digitalWrite(SOL_MOTOR_IN1, LOW);
digitalWrite(SOL_MOTOR_IN2, HIGH);
digitalWrite(SAG_MOTOR_IN3, LOW);
digitalWrite(SAG_MOTOR_IN4, HIGH);
analogWrite(SOL_MOTOR_ENA, motorHizi);
analogWrite(SAG_MOTOR_ENB, motorHizi);
}
void solaGit() {
digitalWrite(SOL_MOTOR_IN1, LOW);
digitalWrite(SOL_MOTOR_IN2, HIGH);
digitalWrite(SAG_MOTOR_IN3, HIGH);
digitalWrite(SAG_MOTOR_IN4, LOW);
analogWrite(SOL_MOTOR_ENA, motorHizi);
analogWrite(SAG_MOTOR_ENB, motorHizi);
}
void sagaGit() {
digitalWrite(SOL_MOTOR_IN1, HIGH);
digitalWrite(SOL_MOTOR_IN2, LOW);
digitalWrite(SAG_MOTOR_IN3, LOW);
digitalWrite(SAG_MOTOR_IN4, HIGH);
analogWrite(SOL_MOTOR_ENA, motorHizi);
analogWrite(SAG_MOTOR_ENB, motorHizi);
}
void motorDurdur() {
digitalWrite(SOL_MOTOR_IN1, LOW);
digitalWrite(SOL_MOTOR_IN2, LOW);
digitalWrite(SAG_MOTOR_IN3, LOW);
digitalWrite(SAG_MOTOR_IN4, LOW);
analogWrite(SOL_MOTOR_ENA, 0);
analogWrite(SAG_MOTOR_ENB, 0);
}
void otomatikModKontrol() {
int minMesafeLeft = mesafeLeft;
int minMesafeRight = mesafeRight;
if (minMesafeLeft < MIN_MESAFE || minMesafeRight < MIN_MESAFE) {
motorDurdur();
delay(300);
int solOndekiMesafe = 100;
for (int aci = 90; aci >= 0; aci -= 30) {
servoLeft.write(aci);
delay(100);
int mesafe = ultrasonikMesafeOlc(TRIG_PIN_LEFT, ECHO_PIN_LEFT);
if (mesafe < solOndekiMesafe && mesafe > 0) {
solOndekiMesafe = mesafe;
}
}
servoLeft.write(90);
int sagOndekiMesafe = 100;
for (int aci = 90; aci <= 180; aci += 30) {
servoRight.write(aci);
delay(100);
int mesafe = ultrasonikMesafeOlc(TRIG_PIN_RIGHT, ECHO_PIN_RIGHT);
if (mesafe < sagOndekiMesafe && mesafe > 0) {
sagOndekiMesafe = mesafe;
}
}
servoRight.write(90);
delay(200);
if (mesafeLeft < MIN_MESAFE && mesafeRight < MIN_MESAFE) {
geriGit();
delay(1000);
if (solOndekiMesafe > sagOndekiMesafe) {
solaGit();
delay(800);
}
else {
sagaGit();
delay(800);
}
}
else if (mesafeLeft < MIN_MESAFE) {
sagaGit();
delay(800);
}
else if (mesafeRight < MIN_MESAFE) {
solaGit();
delay(800);
}
sonYonDegisimZamani = millis();
ileriGit();
}
else {
ileriGit();
if (millis() - sonYonDegisimZamani > 6000) {
if (random(100) < 15) {
motorDurdur();
delay(100);
int yon = random(1, 3);
if (yon == 1) {
solaGit();
} else {
sagaGit();
}
delay(random(300, 600));
sonYonDegisimZamani = millis();
ileriGit();
}
}
}
}
0 and 1 pins used by esp32cam
SoftwareSerial dfpSerial(DFP_TX_PIN, DFP_RX_PIN);
You have the pins backwards
SoftwareSerial(rxPin, txPin, inverse_logic)
