Fernbedienungen mit Joystick

After first experiences with the robust Robot Car and our remote control with the LCD-Keypad Shield, where we get the sent code displayed on the display, I would like to show further possibilities for a self-built remote control with our Micro Controllers and electronic components. In this case, the 433 MHz transceiver HC-12 is used again.

In the case of the preliminary considerations in the last episode, we had already taken into account that we also want to connect an analog joystick and then calculate the code from the values for the x and y axis. The values in the case of a 10-bit analog input are between 0 and 1023, each with approx. 511 in the middle position. For the y-direction (later forward and backward travel) we divide this value by 100 and thus get 11 driving stages from 0 to 10 with standstill at 5. In principle, in addition to the center position, the x-axis also provides 5 steps in each direction. How we implement the code in driving commands is part of the Sketches for the Robot Car.

Hardware 

Number Component
1 Mikrocontroller Board with ATmega328P, ATmega16U2, compatible with Arduino UNO R3
or Nano V3.0 mit Atmega328 CH340! 100% Arduino kompatibel mit Nano V3

or Nano V3.0 with FT232RL chip and ATmega328! 100% Arduino Nano V3 compatible
2 433 MHz transmitter/receiver module HC-12 (unfortunately no longer available)
1 PS2 Joystick Shield Game Pad Keypad V2.0 for Arduino
or KY-023 Joystick Module for Arduino UNO R3 u.a. MCUs
Breadboard, jumper cable, small material
where applicable Case/Chassis from the 3D printer

In the first step, using the HC-12 modules from the last blog, we will replace the LCD-Keypad Shield with a joystick. Depending on the micro controller used, you have the choice between the Joystick Shield for Arduino Uno and compatible MCs and the joystick module, which is suitable for all Micro Controllers with two analog inputs.

The Joystick Shield for all Uno compatible Micro Controllers has a very well-designed layout with many key buttons, the joystick and many connection options.  In particular, the usual connections are also accessible. At the top left of the picture, you can see slots for the nRF24 transceiver, which we will use next time. First, however, we use the ports 3V3, GND, D13, and D12 for our 433 MHz transmitter HC-12.

Picture 1: Joystick Shield for Uno and Pin-compatible Micro Controller

Attention: The inscription leads to the assumption that you can also connect the Nokia5110 display directly here. Here you have to check the exact pin assignment. I could not directly plug in my display, but could connect it with jumper cables.

But we want to join the HC-12 module here. If you need large ranges, you should supply the HC-12 with 5V operating voltage, but in the near range of a remote control for Robot Cars, the 3.3V, simply plug in and adjust the code from the last time due to the changed pin assignment of SoftwareSerial. The line is then

SoftwareSerial mySerial (12, 13); // RX, TX


And the part with the calculation of the code for remote control is very much more scarce when using the joysticks than in our solution with the Keypad. From the pin assignment of the Shields:
Taster A = Digital D2
Taster B = Digital D3
Taster C = Digital D4
Taster D = Digital D5
Taster E = Digital D6
Taster F = Digital D7
Joystick button (push joystick from above) = Digital D8
Joystick x-Axis = Analog A0
Joystick y-Axis = Analog A1

we first need only the analog inputs A0 and A1.

A note still to the constant factor I inserted. In the Joystick Shield there is a sliding switch -in the picture at the bottom left- , on which you can change the voltage for the joystick between 3.3V and 5 V. At 3.3V (as used by me) is the factor=1.5. At 5V, the factor=1.
If someone uses the sketch with a different MCU with a 12-bit analog-to-digital converter, the factor = 0.25 is set.

Picture 2: Micro Controller with Joystick Shield and HC-12 Transceiver


Here is the sketch for remote control:


/*
Joystick als Motor Controller, Stillstand = 505
je 5 Stufen vor/zurück, je 5 Stufen rechts/links
*/

#include <SoftwareSerial.h>

//Anschlussbelegung für den HC-12 Transceiver
SoftwareSerial mySerial(12, 13); //RX, TX

int x = 5; // x-Achse = links/rechts
int y = 5; // y-Achse = vor/zurück
int joybutton = 8; // Joystick button
float faktor = 1.5; // für Anpassung bei unterschiedlicher Spannung oder ADC

void setup() {
pinMode(joybutton,INPUT_PULLUP);
Serial.begin(115200);
mySerial.begin(9600);
}

void sendcode() {
mySerial.println(100*y + x);//send code for motor
delay(100); // little delay for next button press
}

void loop() {
float A0 = faktor * analogRead (0);
float A1 = faktor * analogRead (1);
bool button = digitalRead(joybutton);

Serial.print("x-Achse: ");
Serial.print(A0);
Serial.print("y-Achse: ");
Serial.print(A1);
Serial.print(" Button pressed ");
Serial.print(button);

x = int(A1/100);
y = int(A0/100);

Serial.print(" Code = ");
Serial.println(100*y + x);
sendcode();
}
 

A small tip in between: If the Robot Car does not respond to the signals, then often the question arises, is it caused by the remote control (with the transmitter) or the receiver? We can easily check the remote control with an HC-12 module on the PC.

I still had a USB serial adapter of type CP2102 Breakout. Combine VCC, GND and TX/RX via cross, start new instance of the Arduino IDE, pre-select COM port and open Serial Monitor with 9600 baud. If there is the code 505 or any value between 0 and 1010 is displayed, at least the remote control is in order.

Picture 3: HC-12 to USB-Serial-Adapter

The same remote control can also be realized with the joystick module. For the two axes two analog inputs are needed and for the joystick button a digital input is required on the micro controller. The micro controllers of the Arduino family are also suitable with all compatible MCUs, ESP32 and-with the aid of an analog-digital converter-the Raspberry Pis and the boards with ESP8266 (which has only one analog input by design). I decide this time for the Nano, which is less demanding with regard to the supply voltage.

Picture 4: Joystick module with contact bar

My circuit:

Picture 5: Nano on board (half breadboard) with HC-12 transceivers and joystick module


Sketch Remote Control Nano_Joystick_HC12

/*
Joystick als MotorController, Stillstand = 505
je 5 Stufen vor/zurück, je 5 Stufen rechts/links
*
*/

#include <SoftwareSerial.h>

//Anschlussbelegung für den HC-12 Transceiver
SoftwareSerial mySerial(2, 3); //RX, TX

int x = 5; // x-Achse = links/rechts
int y = 5; // y-Achse = vor/zurück
int code = 505;
int joybutton = 4; // Joystick button
float faktor = 1.0; // für Anpassung bei unterschiedlicher Spannung oder ADC

void setup() {
pinMode(joybutton,INPUT_PULLUP);
Serial.begin(115200);
mySerial.begin(9600);
} //end of setup

void sendcode() {
mySerial.println(100*y + x);//send code for motor
delay(100); // little delay for next button press
} // end of endcode

void loop() {
float A6 = faktor * analogRead (6);
float A7 = faktor * analogRead (7);
bool button = digitalRead(joybutton);

Serial.print("x-Achse: ");
Serial.print(A6);
Serial.print("y-Achse: ");
Serial.print(A7);
Serial.print(" Button pressed ");
Serial.print(button);

x = int(A6/100);
y = int(A7/100);
code = 100*y + x;
Serial.print(" Code = ");
Serial.println(code);
sendcode();

} //end of loop



Finally, I would like to show you how your Robot Car can also drive with a Nano, the HC-12 and a "naked" engine driver IC L293D.

I failed in my attempt to create the schematic in Fritzing. This becomes just as confusing as the image of the finished structure. That's why I chose a table with the connection assignment. If you make changes here, you'll need to adjust the sketch as usual.

 

Figure 6: Pin assignment of the motor driver IC L293D

ENABLE 1 1 Nano Pin 7 Nano 5V 16 Vss
INPUT 1 2 Nano Pin 5 Nano Pin 10 15 INPUT 4
OUTPUT 1 3 left motor right motor 14 OUTPUT 4
GND 4 13 GND
GND 5 Battery - Nano GND 12 GND
OUTPUT 2 6 left motor right motor 11 OUTPUT 3
INPUT 2 7 Nano Pin 6 Nano Pin 9 10 INPUT 3
Vs 8 Battery + Nano Pin 8 9 ENABLE 2

Table 1: Connection assignment of the motor driver IC L293D

 

Figure 7: Pin assignment of the 433 MHz transceivers HC-12

HC-12 Nano
SET not connected
TXD Nano Pin 12 SoftwareSerial RX
RXD Nano Pin 11 SoftwareSerial TX
GND Nano GND
VCC Nano 3,3V oder 5V (greater range)

Table 2: Connection assignment of the 433 MHz transceivers HC-12

When programming is complete and the USB plug is pulled, the Nano can also be connected via Vin to the power supply of the motors as shown in the following image.

Figure 8: twin-engine robot car with Nano, L293D and HC-12

The wiring clearly shows how much more comfortable it is to use a motor driver shield; most contacts are then made by plugging in the micro controller.

Next time I will replace the HC-12 modules, which unfortunately are no longer in the range, with the nRF24L01 modules. These components are also to be programmed  as transmitters as well as receivers. Therefore, the term transceiver comes as a combination of transmitter and receiver.


AmateurfunkFür arduino

Leave a comment

All comments are moderated before being published