9 years, 10 months ago.

How do you pull a pin to "high"?

I'm very very new to mbed and so I have a fairly newbie question.

So /media/uploads/mentality2/main.cpp is my code. It's modified from the original template as I couldn't get that working.

My LED is blinking as it should be when the pin is low (using USB) and it's measured to be around 0V. I use a resistor and connect it to the VIN pin and it's reading 4.9V, but it's still blinking. So either something's wrong with my code, or I'm doing something wrong?

My "push user button and make LED blink program works fine".

Thanks

1 Answer

9 years, 10 months ago.

PC_15 is #define'd as a constant, it's a handy label so we don't have to remember the magic number 0x2F which is that pins offset from the base address of the GPIOs in memory. Instead, use DigitalIn to read that pin:

DigitalIn myinput(PC_15);

if (myinput==1) {
    myled=1;
}

Thanks! I just tried that but now it's even more confusing. The voltage is low and the led is on, so shouldn't it be high? And I am still unable to pull it up to high.

posted by Denise Tang 10 Jan 2015

Maybe the source of the confusion is the scope of the while(1) loop? Your code checks PC_15 one time only, straight after reset, then gets stuck with either the LED on, or stuck flashing. Try this:

#include "mbed.h"

DigitalOut myled(LED1);
DigitalIn myinput(PC_15);

int main() {
    while (1) {
        if (myinput == 1) {
            myled = 1;
        }    
        else {
            myled = 1; // LED is ON
            wait(0.1); // 100 ms
            myled = 0;
            wait(0.1);
            myled = 1;
            wait(0.1); 
            myled = 0; // LED is OFF
            wait(1.0); // 1 sec
        }
    }
    
} 
posted by David Lowe 10 Jan 2015

I tried that and here are the results: from my understanding of that code, if the pin reads high, the LED should stay on. If it's low, the LED should blink. I measured the ground to PC15 and it is around -80mV. Wouldn't this be considered low? If so, Why is the LED constantly on, as it is right now? Does that mean something's wrong with my code or my logic?

Also, what is the threshold for high/low? What is considered high or low out of 4.9V? Or am I approaching this the wrong way.

Lastly, how do I change the pin readings themselves? Since that pin reads close to 0V, I used a resistor in between PC_15 and the 5V outlet and it gives me a reading of 4.9V, yet there are no changes. Am I doing something wrong? Is this the correct or not correct way of doing it?

TLDR: I am trying to read something at a pin (any pin) and make something happen (make the LED do things). Afterwards I will try multiple pins but I'm trying to get just 1 pin down first.

posted by Denise Tang 11 Jan 2015

Hi - I have tried & confirmed the behaviour of the above code on my Nucleo: LED on (PC_15 tied to 3V3) or flashing (PC_15 tied to ground). Have you identified the correct pin on the Morpho header (6th pin up on the left-most column)?

What do you mean by the "5V outlet"? If it is some external 5V signal, then be sure to connect grounds as well.

I cannot think what else might explain your problem!

posted by David Lowe 11 Jan 2015