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

Thread: NewPing not working with teensy 4.1

  1. #1
    Junior Member
    Join Date
    Sep 2020
    Posts
    3

    NewPing not working with teensy 4.1

    Hi, I search and could not found any other topic regarding the NewPing library and the teensy 4.1.

    Hi have a 4 pins HC-SR04 connected to my teensy. I have used this library many times on other Arduino board and never had problems.

    When I compile the project I get those errors

    Code:
    NewPing.cpp:19:17: error: cannot convert 'volatile uint32_t* {aka volatile long unsigned int*}' to 'volatile uint8_t* {aka volatile unsigned char*}' in assignment
      _triggerOutput = portOutputRegister(digitalPinToPort(trigger_pin)); // Get the output port register for the trigger pin.
    and

    Code:
    NewPing.cpp:20:13: error: cannot convert 'volatile uint32_t* {aka volatile long unsigned int*}' to 'volatile uint8_t* {aka volatile unsigned char*}' in assignment
      _echoInput = portInputRegister(digitalPinToPort(echo_pin));         // Get the input port register for the echo pin.

    To reproduce this simply had the newping to any teensy 4.1 project

    Code:
    #include <NewPing.h>
    
    NewPing sonar_front(6, 5, 200);
    
    void setup() {
    
    }
    
    void loop() {
    
    }
    I have looked around and I saw that that the _triggerOutput and _echoInput are defined uint32_t if defined PARTICLE but not TEESYDUINO. I try to force them to uint32_t but got may error downstream.

    Before going in a deep debug of the lib, is there something I'm missing here ?

    I saw that this lib is supposed to be compatible with teensy board, but is the 4.1 to recent for them ?

    Is there an other lib for using 4 pin sensor , the ping lib is made for 1 trig/echo pin ....

    Thanks for the help !

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    12,380
    Seems like an edit to the #ifdef in the library code where PARTICLE is given an exception - the same exception is needed for :: defined(__IMXRT1062__)
    > that should fix for T_4.x's

  3. #3
    Junior Member
    Join Date
    Sep 2020
    Posts
    3

    Post

    Thanks for the quick answer !

    So I had to add this #ifdef in the NewPing.h and change a line in the NewPing.c
    Code:
    	#if defined(__IMXRT1062__)
    		_triggerMode = portModeRegister(digitalPinToPort(trigger_pin)); // Get the port mode register for the trigger pin.
    	#else
    		_triggerMode = (uint8_t *) portModeRegister(digitalPinToPort(trigger_pin)); // Get the port mode register for the trigger pin.
    	#endif
    But my sensor does not seems to work. Not quite sure if it's my voltage divider on the echo pin (5v to 3.3V), the lib or my sensor.

    I will investigate a bit more, but it compile well.

    If it compile it should work right ?! (joke)

  4. #4
    Junior Member
    Join Date
    Sep 2020
    Posts
    3
    All was good, the newping lib from the arduino repo is no good with T4. Always return 0....

    Finnaly got it to work using this lib :
    https://github.com/mjs513/NewPing_t4

    thanks to mjs513. This newpin lib works well on my 4.1.

    Cheers !

Posting Permissions

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