Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: T3.6 MOSFET switch problem

  1. #1

    T3.6 MOSFET switch problem

    Hello everyone,

    I'm currently failing on a task that initially seemed easy... at least that is was I thought :-)

    I have a sensor that is powered by a powerbank. I want to switch the sensor's power supply via the Teensy (it should be switched of over night to save power). When turned on, the sensor consumes between 70mA and 100mA at 5V.

    My plan is to use a MOSFET for this purpose but at the moment the sensor remains ON even when I remove the voltage from the gate using a IRLB8721 N-Channel MOSFET. My knowledge of the subject is somewhat rusty, as it seems to me, because reading about this topic confused me a lot with things like voltage difference, N-Channel vs. P-Channel MOSFET, low side vs high side switching and so on :-)

    Would someone be kind enough to help me solve the problem? To be clear: This is more of a general question than a Teensy related one, because currently the Teensy isn't even involved, i'm just using it's 3.3V output pin for the gate volatage. In the attachment you will find a simple drawing of the part of the bigger circuit for the project as well as a simple photo of the sensor.



    Thank you in advance,
    Martin
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	IMG_20200508_122429.jpg 
Views:	22 
Size:	66.5 KB 
ID:	20022   Click image for larger version. 

Name:	IMG_20200508_122108.jpg 
Views:	14 
Size:	92.6 KB 
ID:	20020  

    Click image for larger version. 

Name:	IMG_20200508_122128.jpg 
Views:	15 
Size:	141.9 KB 
ID:	20021  

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    22,637
    Schematic looks like it ought to work.

    I don't see the mosfet or resistors in your photo. Any chance you may have swapped the drain & source when wiring it up? These mosfets have a diode built in, between the drain and source. If connected backwards, current can flow through the diode when the mosfet should be turned off.

  3. #3
    Hello Paul,

    sorry, I forgot the actual photo of the curcuit. And after another quick test, I just found the reason for problem.

    The Sensor is connected via the Teensy USB Host port, but leaving the 5V pin unconnected, because I want the sensor to be powered only by the external powerbank. If I unplug the ground pin of the USB host port too, I can programmically switch the sensor on and off as expected. Well, with this understanding, I can go on.

    Thank so so far :-)

    Martin

    P.S. The USB host function of the Teensy 3.6 really saved this project, so thank you for your work.
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	IMG_20200508_144342.jpg 
Views:	13 
Size:	173.2 KB 
ID:	20023  

  4. #4
    Hey Paul,

    I solved the problem mentioned, but I stumbled across another one. I now can power up my sensor with the MOSFET, that's nice. But when doing so, I'm not able to establish a serial connection with it, my program stops at "userial.begin". If I connect ground of the sensor directly to ground of my circuit, everything works fine, the program gets into Loop and I'm able to read data from it.

    Do you have any clue, what could cause this?

    Code:
    #include <TimeLib.h>
    #include <TimeAlarms.h>
    
    // USB host settings
    #include "USBHost_t36.h"
    uint32_t format = USBHOST_SERIAL_8N1;
    USBHost myusb;
    USBSerial userial(myusb);
    
    
    
    const byte numChars = 32;
    char receivedChars[numChars];   // an array to store the received data
    boolean newData = false;
    char buffer[150];
    
    void setup() {
        
      Serial.begin(115200);
    
      Serial.println("I'm ready");
    
      pinMode(13,OUTPUT);
      digitalWrite(13,HIGH);
      pinMode(5,OUTPUT);
      digitalWrite(5,HIGH);
    
      myusb.begin();
      Serial.println("myusb.begin finished");
      myusb.Task(); 
      Serial.println("myusb.Task finished");
      userial.begin(115200);
      Serial.println("userial.begin finished");
      
    }
    
    void loop() {
      
     if (userial.available() > 0){
          while (userial.available()) {
            recvWithEndMarker();
            readSensor();
         } 
      } 
      
      digitalClockDisplay();
      Alarm.delay(1000);
      
    }
    
    // RTC clock functions
    
      void setTimeFromRtc(){
        setSyncProvider(getTeensy3Time); 
      }
    
      time_t getTeensy3Time()
    {
      return Teensy3Clock.get();
    }
    
    
    void digitalClockDisplay() {
      // digital clock display of the time
      Serial.print(hour());
      printDigits(minute());
      printDigits(second());
      Serial.println();
    }
    
    void printDigits(int digits) {
      Serial.print(":");
      if (digits < 10)
        Serial.print('0');
      Serial.print(digits);
    }
    
    void readSensor() {
    
      digitalWrite(13, HIGH);
      delay(50);
      digitalWrite(13, LOW);
      delay(50);
      digitalWrite(13, HIGH);
      delay(50);
      digitalWrite(13, LOW);
      delay(50);
      digitalWrite(13, HIGH);
      delay(50);
      digitalWrite(13, LOW);
    
        if (newData == true) {      
          
          Serial.println(receivedChars); 
    
          newData = false;
        }
    }
    
    
    
    void recvWithEndMarker() {
        static byte ndx = 0;
        char endMarker = '\n';
        char rc;
       
        while (userial.available() > 0 && newData == false) {
            rc = userial.read();
    
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                ndx = 0;
                newData = true;
            }
        }
    }
    Thank you,
    Martin
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	IMG_20200512_100334.jpg 
Views:	12 
Size:	73.3 KB 
ID:	20072  

  5. #5
    Senior Member
    Join Date
    May 2017
    Posts
    106
    It seems you switch off your GND-line by MOSFET, so UART can't work without GND connection.

    Instead switch the 5V-line to your sensor. Use a p-channel MOSFET then.

  6. #6
    I got myself a p-channel MOSFET and everything works fine now. Thank you very much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •