I'm attempting to write a class that inherits from the Print class. I only need serial output.

However, I'm having difficulty locating any reference on how to specify which pin on the ATMega my class will use as output of the write(...) functions.

How does the Print class know where to send output to ??

Thanks in advance.


You need to do far more than just "specify a pin". You need to write the software that transmits data through that pin in your chosen format.

You cannot send data through a pin. You can only set a pin HIGH or set a pin LOW. It is the sequence (and most importantly the timing) of setting a pin HIGH and LOW that constitutes sending data.

So you have to write the software that sets the pin HIGH and LOW in the right way and at the right time.

Fortunately sending is far simpler than receiving, as you can see if you look at the source code to SoftwareSerial.

Specifying the pin itself though is simple:

class MyClass : public Print {
        uint8_t _pin;

        MyClass(uint8_t pin);
        size_t write(uint8_t data);

MyClass::MyClass(uint8_t pin) {
    _pin = pin;

size_t MyClass::write(uint8_t data) {
    // In here you do whatever you have to in order
    // to manipulate _pin in the right way to send your data.

    // Don't forget to return the number of bytes written (there
    // is only one, so either 1 for "it got sent" or 0 for "it
    // didn't get sent".
    return 1;

It can be quite useful to have a begin() function for setting things up, such as configuring the right pinMode() for _pin, and maybe setting a baud rate like Serial does (although for simplicity you may choose to just have a single fixed baud rate, which means you can pre-calculate delay loops etc).

  • It seems I got past the "brain block"... Just had to walk away from it for a while and the procedure just came to me. Thanks to all. Jan 30 '17 at 12:02

The parent class (your class, in this case) provides read() and write() functions which in turn, know what ports to use. Presumably those were provided to the parent class' constructor. Now the Print class just has to know the standard function names.

  • Thank you, JRobert. Apparently, it's specifying the "specifying which pin to use in my class' constructor" that's causing my "obvious brain block". I don't want to use the hardware serial so I can leave normal TX and RX available for re-programming. I tried inheriting from SoftwareSerial, but it apparently requires both TX and RX to be specified, and I only need Jan 30 '17 at 1:59
  • I only need my class to send data (not receive). I hope I stated that clear enough. Jan 30 '17 at 2:07
  • It won't hurt to implement the read function anyway; make it an empty one if you wish. But I'd expect Print would refer to it (statically) and you'd get an undefined function error at compile time if no such function is present.
    – JRobert
    Jan 30 '17 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.