Help with pushbutton/LED needed

Post here first, or if you can't find a relevant section!
TurboTimmy
Posts: 22
Joined: Wed Dec 22, 2021 9:43 pm

Help with pushbutton/LED needed

Post by TurboTimmy »

Hello all

I have been playing about with Arduino for quite a while now. I love using the Uno/Nano/ProMicro. For my next project I wanted to use something that could process information a little faster. I was drawn to the BlackPill STM32F401CCu6 as it stated that it was usable with Arduino IDE. Perfect, or so I thought.

So after a few days of fighting and head scratching, I finally managed to get the board connected and accepting a very basic blink sketch. I Followed the tutorial here:

https://www.sgbotic.com/index.php?dispa ... page_id=49

Now it was claimed that this board would simply make use of the USB port via DFU. I have not yet got this to work, but I have managed to use a nodeMCU board as a uart adaptor to download a sketch. Not perfect, but it is a start at least.

Now to my question.

Now that I have the blink sketch up and running, I want to be able to now use a button to activate the LED while pressed, and turn off when the button is released. I cant seem to get this really simple sketch to work. Here is code that I am trying:

Code: Select all

void setup() {

pinMode(PC13, OUTPUT);
pinMode(PB2  , INPUT_PULLUP);

}

void loop() {

if (digitalRead(PB2 == LOW) );
 { digitalWrite(PC13, HIGH); }
 
 }
As you can see, it is extremely simple but I cannot get it to work. Could anyone please help?

I also think that my board/Arduino IDE could be better setup. If someone could point me in the right direction to a tutorial that will get me setup better, I would greatly appreciate it.

I would really like to be able to use a tft display later down the line. I have found this tutorial, but it requires the use of libraries that have been adapted for STM32.

https://www.electronics-lab.com/project ... duino-ide/

Would I need to setup ArduinoIDE in a specific way to be able to do this?

Thank you all in advance.

Tim
ag123
Posts: 1653
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Help with pushbutton/LED needed

Post by ag123 »

Code: Select all

if(digitalRead(PB2 == LOW)) { 
should be coded as

Code: Select all

if(digitalRead(pin) == LOW) { 
be sure to check for the correct pin that you are trying to read and that you are looking for LOW (i.e. 0)
and do check the schematics for your board, some of them light up the led if it is LOW.
TurboTimmy
Posts: 22
Joined: Wed Dec 22, 2021 9:43 pm

Re: Help with pushbutton/LED needed

Post by TurboTimmy »

ag123 wrote: Wed Dec 22, 2021 10:30 pm

Code: Select all

if(digitalRead(PB2 == LOW)) { 
should be coded as

Code: Select all

if(digitalRead(pin) == LOW) { 
be sure to check for the correct pin that you are trying to read and that you are looking for LOW (i.e. 0)
and do check the schematics for your board, some of them light up the led if it is LOW.
Thank you for your really fast reply. I appreciate it. I have altered my code and it didnt work. To save burning anything out, I have changed the LED and replaced with Serial.print. Here is my code is it is:

Code: Select all

int Switch = PB2;


void setup() {

pinMode(Switch, INPUT_PULLUP);

Serial.begin(9600);

}

void loop() {

if (digitalRead(Switch) == LOW);
 { Serial.println("Pressed - PullUP"); delay(150); }


}
All I get in serial monitor is a constant stream of "Pressed - Pullup". It doesnt even stop when I press and hold the button. Any thoughts? My switch/button is wired in like this:

switch pin 1 ------ B2
switch pin 2 -------GND

I hope that makes sence. This normally works on my Arduino boards just fine. I can't understand why it wont work on the Black Pill.
dannyf
Posts: 446
Joined: Sat Jul 04, 2020 7:46 pm

Re: Help with pushbutton/LED needed

Post by dannyf »

you have one too many ';' there.
TurboTimmy
Posts: 22
Joined: Wed Dec 22, 2021 9:43 pm

Re: Help with pushbutton/LED needed

Post by TurboTimmy »

It would appear that I may have something working.

3v ------ Switch pin 1

Switch pin 2 ---- PB2

Code is as follows:

Code: Select all

#define Button PB2


void setup() {

pinMode(Button, INPUT);

Serial.begin(9600);
 

}

void loop() {
 int ButtonState = digitalRead(Button);
 if(ButtonState == HIGH)
 {
  Serial.println("Pressed"); delay(150);
 }


}
It isn't quite how I would like it, but it works for now. I would much rather use a switch to gnd and make use in INPUT_PULLUP.

I am also sick of having to switch between UART to install the sketch, and then switching over to USB to serial monitor. I really need to sort it so that I can upload straight from USB.
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 26
Location: Prudnik, Poland

Re: Help with pushbutton/LED needed

Post by GonzoG »

TurboTimmy wrote: Wed Dec 22, 2021 10:55 pm

Code: Select all

...
if (digitalRead(Switch) == LOW);
 { Serial.println("Pressed - PullUP"); delay(150); }


}
Your problem is with ";" at the end of "if" line.
Written like this, if statement ends at ";", so Serial.println() is not inside "if" and will execute every time.

Try this:

Code: Select all

#define SWITCH PB2

void setup() {
	pinMode(STWITCH, INPUT_PULLUP);
	Serial.begin();
}

uint8_t prevState = 1;

void loop(){
	uint8_t state = digitalReadFast(digitalPinToPinName(SWITCH));
	if( state != prevState)
	{
		prevState = state;
		if(state == 0)
			Serial.println("Pressed");
		else
			Serial.println("Released");
	}
}
Last edited by GonzoG on Thu Dec 23, 2021 6:19 pm, edited 1 time in total.
TurboTimmy
Posts: 22
Joined: Wed Dec 22, 2021 9:43 pm

Re: Help with pushbutton/LED needed

Post by TurboTimmy »

I see your point. Thank you for explaining it. Managed to get it downloaded to the board and working, although the Serial.print values seem to be backward. Anyway, now that I have a working button press, it is time to move onto the next code snippet. I am trying to put together small examples of different things that I will ultimately put together. So we have button press sorted, now to move onto ST7735 display.

Thank you all fro your help. I have no doubt there will be more posts to come.
ag123
Posts: 1653
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Help with pushbutton/LED needed

Post by ag123 »

TurboTimmy wrote: Wed Dec 22, 2021 11:59 pm I am also sick of having to switch between UART to install the sketch, and then switching over to USB to serial monitor. I really need to sort it so that I can upload straight from USB.
try with an st-link v2
https://www.adafruit.com/product/2548
https://octopart.com/search?q=st-link+v2
you could also learn to debug as you go along
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 26
Location: Prudnik, Poland

Re: Help with pushbutton/LED needed

Post by GonzoG »

TurboTimmy wrote: Wed Dec 22, 2021 10:21 pm
Now it was claimed that this board would simply make use of the USB port via DFU. I have not yet got this to work, but I have managed to use a nodeMCU board as a uart adaptor to download a sketch. Not perfect, but it is a start at least.
I've missed that part...
It does work with DFU, but you need to manually reset it into DFU mode -> reset it with BOOT0 button pressed, then release BOOT0 button a second or 2 after reset button.
I've found that if you put your finger on USB socket, it works much better (almost always boots into DFU).
TurboTimmy
Posts: 22
Joined: Wed Dec 22, 2021 9:43 pm

Re: Help with pushbutton/LED needed

Post by TurboTimmy »

GonzoG wrote: Fri Dec 24, 2021 5:37 pm
TurboTimmy wrote: Wed Dec 22, 2021 10:21 pm
Now it was claimed that this board would simply make use of the USB port via DFU. I have not yet got this to work, but I have managed to use a nodeMCU board as a uart adaptor to download a sketch. Not perfect, but it is a start at least.
I've missed that part...
It does work with DFU, but you need to manually reset it into DFU mode -> reset it with BOOT0 button pressed, then release BOOT0 button a second or 2 after reset button.
I've found that if you put your finger on USB socket, it works much better (almost always boots into DFU).
Thanks for that. I have managed to do that since day one, but I cannot for the life of me get it to download a sketch via the USB socket (on the board). I always have to use UART. It was claimed that I could use the USB socket, but this doesnt seem to be the case. So, to use the board I have to:

1: Connect via UART to gnd, 3.3v, A9, A10
2: Download sketch
3: Dissconnect UART
4: connect USB-C to run the program (I use alot ok serial read/write)

I would like to do away with having to switch between UART and USB, and be able to download via the USB socket (in the same we I do with the Arduino boards)

On a side note, I cannot download a new sketch to the board until I have used STM32 CubeProgrammer to earse everything first. Not sure what's going on there.
Last edited by TurboTimmy on Fri Dec 24, 2021 6:37 pm, edited 1 time in total.
Post Reply

Return to “General discussion”