Skip to content

digitalWrite(pin, HIGH) with GPIO 4 doesn't goes HIGH #1204

@nickoloss

Description

@nickoloss

Hardware:

Board: ESP32 PICO Dev Module
Core Installation/update date: 11/March/2018
IDE name: Arduino IDE 1.85
Flash Frequency: 40Mhz
Upload Speed: 921600

Description:

Describe your problem here

Sketch:

----------------- ElectricEngine.hpp ---------------------


#ifndef ElectricEngine_hpp
#define ElectricEngine_hpp

#include "Arduino.h"


#define clockwise_pin 4
#define counterclockwise_pin 12
#define pwm_pin 24

class ElectricEngine {
    
    int oldDirection = 0;
    int pwm_pin_channel = 1;
    void setClockwiseDirection();
    void setCounterclockwiseDirection();
    
public:
    ElectricEngine();
    void run(int speed, int direction);
    void stop();
};

#endif /* ElectricEngine_hpp */

----------------- ElectricEngine.cpp ---------------------

#include "ElectricEngine.hpp"

ElectricEngine::ElectricEngine() {
    
    
    pinMode(pwm_pin, OUTPUT);
    pinMode(clockwise_pin, OUTPUT);
    pinMode(counterclockwise_pin, OUTPUT);
    // Initialize channels
    // channels 0-15, resolution 1-16 bits, freq limits depend on resolution
    // ledcSetup(uint8_t channel, uint32_t freq, uint8_t resolution_bits);
    ledcAttachPin(pwm_pin, pwm_pin_channel);
    ledcSetup(pwm_pin_channel, 500, 8);
    

}

void ElectricEngine::run(int speed, int direction) {
    
    if (direction == 1 ) {
        setClockwiseDirection();
        ledcWrite(pwm_pin_channel, speed);
        oldDirection = 1;
    } else if (direction == -1 ) {
        setCounterclockwiseDirection();
        ledcWrite(pwm_pin_channel, speed);
        oldDirection = -1;
    }
    

};

void ElectricEngine::stop() {
    ledcWrite(pwm_pin_channel, 0);
    digitalWrite(counterclockwise_pin, LOW);
    digitalWrite(clockwise_pin, LOW);
};

void ElectricEngine::setClockwiseDirection() {
    digitalWrite(counterclockwise_pin, LOW);
    digitalWrite(clockwise_pin, HIGH);
};

void ElectricEngine::setCounterclockwiseDirection() {
    digitalWrite(clockwise_pin, LOW);
    digitalWrite(counterclockwise_pin, HIGH);
};

---------------------- sketch.ino -------------------

include "ElectricEngine.hpp"

ElectricEngine engine;

setup(){

}

loop(){
engine.run(200, 1);
delay(1000);
engine.stop();
engine.run(200, -1);
delay(1000);

}

Debug Messages:

GPIO 12 goes HIGH as supposed
GPIO 4 stays LOW ---> Should go HIGH

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions