Teensy 4.0 Output stays high

Status
Not open for further replies.

ManMadeDesaster

New member
Hello,

i have a Teensy 4.0 and a nextion display with the io adapter. I use some of the nextion io pins as output (3.3v), which are directly wired to the inputs of the teensy. when i press a button on the display an output gets high and when i release it the output gets low. i programmed the teensy to output 3.3v via a resistor into an optocoupler, which then "switches" the other side to power a led.

this works except the output of the teensy stays high even though the output of the nextion, which goes into the input of the teensy, is low. i tried INPUT_PULLUP as well as INPUT_PULLDOWN but its the same. I have to manually set the inputs low like this: digitalWrite(bwd, LOW);

this is my code (totally not finished beginner noob code :) ):


Code:
#include <AccelStepper.h>
#include <TMC2208Stepper.h>
#include <TMC2208Stepper_REGDEFS.h>
TMC2208Stepper driver = TMC2208Stepper(&Serial1);
AccelStepper stepper(1, 3, 4);    // 1= Easy Driver interface
                                  // Pin 3 Step
                                  // Pin 4 Dir
   
const int EN_PIN = 2;
const int STEP_PIN = 3;
const int DIR_PIN = 4;
const int end_1 = 9;
const int end_2 = 10;
const int focus = 11;
const int shutter = 12;
const int led = 13;
const int fwd = 14;
const int bwd = 15;
const int cycle_stop = 16;
const int cycle_start = 17;
const int set_param = 18;


int ledState = LOW;                 // ledState used to set the LED


unsigned long previousMillis = 0;   // will store last time LED was updated

const long blinkTime = 1000;         // interval at which to blink (milliseconds)

boolean ready, runallowed = false;

void setup() {

Serial.begin(115200);               //Computer
Serial1.begin(115200);              //TMC2208
Serial2.begin(921600);              //Nextion

//while(!Serial);
while(!Serial1);
while(!Serial2);

Serial2.print("page0.t0.txt=\"Verbunden\"");
Serial2.write(0xff);
Serial2.write(0xff);
Serial2.write(0xff);

pinMode(fwd, INPUT_PULLDOWN);          // Vorwärts
pinMode(bwd, INPUT_PULLDOWN);          // Rückwärts
pinMode(cycle_stop, INPUT_PULLDOWN);   // Stoppen
pinMode(cycle_start, INPUT_PULLDOWN);  // Starten
pinMode(set_param, INPUT_PULLDOWN);    // Start/Endpunkt setzen
pinMode(end_1, INPUT_PULLUP);        // Endschalter 1
pinMode(end_2, INPUT_PULLUP);        // Endschalter 2

pinMode(EN_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode(focus, OUTPUT);
pinMode(shutter, OUTPUT);
pinMode(led, OUTPUT);


stepper.setMaxSpeed(1000.0);
stepper.setAcceleration(4000.0);

driver.pdn_disable(1);              // Use PDN/UART pin for communication
driver.I_scale_analog(0);           // Adjust current from the registers
driver.rms_current(700);            // Set driver current 500mA
driver.toff(0x2);                   // Enable driver
driver.mstep_reg_select(1);         // Microstep resolution selected by MSTEP register
driver.microsteps(4);               // [0..256] Set number of microsteps
driver.shaft(0);                    // Drehrichtung Motor

delay (500);
digitalWrite(EN_PIN, LOW);          // Enable driver
digitalWrite(led, HIGH);            // Status LED Pin 13
}

void loop() {
  stepper.run();
  ledTime();
  io();
}

void ledTime(){

unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= blinkTime) {
  // save the last time you blinked the LED
  previousMillis = currentMillis;
  // if the LED is off turn it on and vice-versa:
  if (ledState == LOW) {
      ledState = HIGH;
      } else {
      ledState = LOW;
      }
      
      // set the LED with the ledState of the variable:
      digitalWrite(led, ledState);
      }
}


void io(){
  
  digitalWrite(shutter, LOW);
  digitalWrite(focus, LOW);
  digitalWrite(fwd, LOW);
  digitalWrite(bwd, LOW);
  if(digitalRead(end_1) == LOW){
    stepper.move(100);
  }
  if(digitalRead(end_2) == LOW){
    stepper.move(-100);
  }

  if(digitalRead(set_param) == HIGH){
  }
  

  if(digitalRead(fwd) == HIGH){
    stepper.move(100);
  }
  if(digitalRead(bwd) == HIGH){
    stepper.move(-100);
  }
  if(digitalRead(cycle_stop) == HIGH){
    stepper.stop();
  }
  if(digitalRead(cycle_start) == HIGH){
    cycleStart();
  }
}


void cycleStart(){
    digitalWrite(focus, HIGH);
    digitalWrite(shutter, HIGH);
}

is this normal behaviour? i remember that the output is high as long as the input is high. If the input is low then the output should also get low.
 
This is slightly confusing exactly how are the Teensy, Nextion and optocouplers connected ? Which pin is input from Nextion, and which is the corresponding output from Teensy ?
 
This is slightly confusing exactly how are the Teensy, Nextion and optocouplers connected ? Which pin is input from Nextion, and which is the corresponding output from Teensy ?

all io pins of the nextion adapter are setup as 3.3v outputs, but i dont use all of them. They go straight into pin 14-18 of the teensy, which are set to input. When i press a button on the Nextion, it sends a high signal into one of pin 14-18 of the teensy. When i release the button, it sends a low signal. The teensy then switches an output (either pin 11 or 12) to high, which powers one side of the optocoupler and the optocoupler then switches the other side so the led lights up (the led is just there so i can see if it works, its real purpose is to just short two pins to ground of my camera to take a picture).

if i didnt make any mistakes, this is how i connected them:

wiring.jpg
 
The code checks if cycle_start pin is HIGH and calls cycleStart() that sets the output pins and led high.

You need to check when the cycle_start pin goes low to turn off the leds, or use a timer for that or the cycle_stop input depending on your program logic.

Another problem is that you keep calling cycleStart() for every loop, it should probably only be done when the input transitions from low to high. So the current state should be recorded, then your code can respond to changes.

int cycle_active = 0; /* flag for cycle state */


in the loop you add something like this
Code:
  if( (cycle_active == 1 ) && ( digitalRead(cycle_stop) == HIGH)) {
    cycle_active = 0;
    cycleStop();
  }

  if( (cycle_active == 0 ) && ( digitalRead(cycle_start) == HIGH)) {
    cycle_active = 1;
    cycleStart();
  }
 
The code checks if cycle_start pin is HIGH and calls cycleStart() that sets the output pins and led high.

You need to check when the cycle_start pin goes low to turn off the leds, or use a timer for that or the cycle_stop input depending on your program logic.

Another problem is that you keep calling cycle_start for every loop, it should probably only be done when the input transitions from low to high. So the current state should be recorded, then your code can respond to changes.

int cycle_active = 0; /* flag for cycle state */


in the loop you add something like this
Code:
  if( (cycle_active == 1 ) && ( digitalRead(cycle_stop) == HIGH)) {
    cycle_active = 0;
    cycleStop();
  }

  if( (cycle_active == 0 ) && ( digitalRead(cycle_start) == HIGH)) {
    cycle_active = 1;
    cycleStart();
  }

thank you i will try that :)
 
The Nextion web site states that the I/O adaptor puts out 5v.
The T4 only supports 3.3v devices. You may have "blown up" that input on the Teensy.
 
Status
Not open for further replies.
Back
Top