Hey guys, so i'm making an rc controller with STM32, and I need 2 joysticks. The problem, is that those joysticks work fine with arduino, but with STM32, for example when I mapped them from 0-1023 to -255 - 255, as soon as I barely move the joystick, the value gets to -255 or 255 (in one direction, it never goes more than 250, but ignore this fact so that we can troubleshoot the small range), within 5 degrees or so, so it gives me a dead zone after it reaches the max value, so from those 5° to the limit of the joystick, the value stays the same. So it gives me a very little range of reading. Is there any solutions for that ?
What joysticks?
Please show your code & schematic
@awneil The Arduino Joysticks that come with starter kits, for the code, I did not save it, it's a simple analog read code that maps the values from 0-1023(I know that the STM32F103C8T6 has 12bit ADC but with the arduino board core it's bu default mapped to 10bit) to -255 255. For the circuit : I connected the 5v of the stick to 3.3v (since the STM32 operates at 3.3v and the ADC pins also operate at 3.3v) the GND to GND, and the x axis for example to an adc pin (PB1)
according to your sketch. should we guess it?
What do you mean?
your sketch
I`m not sure about this, but you can use analogReadResolution(12);
Btw, if you variables are not adequate, you may have an overflow... Post the code if you want help.
-255 - 255
-255 to 255 does not fit in an 8 bit variable. If using an 8 bit variable, the value is truncated.
@noobmastha Here is the code before mapping
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width
#define SCREEN_HEIGHT 64 // OLED display height
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
void setup() {
// initialize with the I2C addr 0x3C
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Clear the buffer
display.clearDisplay();
// Display a line of text
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
}
void loop() {
int sensorValue = analogRead(PB1); // read the input on analog pin PB1
display.clearDisplay(); // clear the display
display.setCursor(0,0); // set cursor position
display.println(sensorValue); // print the sensor value
display.display(); // update screen with all of the above graphics
delay(1000); // wait for a second
}
I now use an oled because I can't access the serial monitor because it randomly stopped working.
Now here is the code after mapping
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width
#define SCREEN_HEIGHT 64 // OLED display height
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
void setup() {
// initialize with the I2C addr 0x3C
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Clear the buffer
display.clearDisplay();
// Display a line of text
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
}
void loop() {
int sensorValue = analogRead(PB1); // read the input on analog pin PB1
int mapped = map(sensorValue, 0, 1023, -255, 255);
display.clearDisplay(); // clear the display
display.setCursor(0,0); // set cursor position
display.println(mapped); // print the sensor value
display.display(); // update screen with all of the above graphics
delay(1000); // wait for a second
}
In both cases the stick reaches max value withing some degrees of moving, here is an image illustrating it :
The black circle is the full circle of the joystick, and the red one is the range that I get. Every position outside that red circle can't be used, since the values go to the max / min inside that circle. So I get 255/-255 inside that red circle, out of that red circle, the value stays unchanged. I hope that you understand
I used Bing Chat for the codes because I did not fully learn to use the oled displays
Can you send the picture or link of joystick you are using?
If Adafruit_SSD1306 works with I2C oled display, then you are using old Rogers core for STM32, not the official core?
Those cheap joysticks are not very usable... If you have DMM you can measure how the resistance is changing very fast from max to min value if you move the joystick little bit off the center.
Try to remove delay(1000); from the code and I think you should see some changes when holding joystick little bit off the center position.
Sorry, I did not modify that, but I did remove the delay, when I tested it
If you have DMM you can measure how the resistance is changing very fast from max to min value if you move the joystick little bit off the center.
Maybe that's true, but with arduino (5V), it gave me the full range, so maybe the potentiometers are made for 5V not 3.3V ? If that's true, why do some guys on youtube have a bigger range (Still not full range) like this one : (1) STM32 and JOYSTICK | #38 - YouTube
I also have an old PS3 controller board with 2 joysticks, and apparently the PS3 controller works with 3V according to my multimeter, so I said why don't I use them, but unfortunately they're cloggy and don't return to the center, even WD-40 did not fix that
I took a closer look to your code. Input pin is not defined?
Add:
pinMode(PB1, INPUT);
It didn't change anything
@noobmastha I now remembered, on the Joystick's PCB, there are 2 pads for soldering and they're labeled R1, so I suppose that that's for an SMD Resistor, but what's that resistor used for ?
Are you sure, that ADC is working at all? You can try it with pot instead of joystick. Post the picture of wiring or schematic...
Yep, 100% sure, because I tried with a potentiometer before the joystick. For the wiring, I don't have a picture but here is it :
JOYSTICK > STM32
+5V 3.3V (STM32 ADCs are not 5V tolerant)
GND GND
One of the axis > PB1
I don't know if I mentioned this before, but I'm using the STM32F103C8T6
https://wokwi.com/projects/376208405815634945
As you can see, it works, maybe something is wrong with your hardware or wiring.
I did exactly the same thing as the simulation. Maybe like I said, the joysticks resistance is meant for 5V not 3.3V. And I don't think a joystick simulation is accurate in a simulation since it could be programmed to provide a value according to the position not the resistance. So it works with any board, Microcontroller, Voltage...
No this is nonsense..
Simulation IS accurate, and this is all very basic stuff to debug...I`m out .