Page 1 of 2

Z-uno is running much slower on PC power than on charger

Posted: 17 Feb 2018 00:43
by risca
Hello,

I'm creating a sketch to control my projector at home. Unfortunately, my projector requires a (9:th) parity bit for UART communication (see other topic), so I decided to bitbang the protocol. Here's the code:

Code: Select all

// LED pin number
#define LED_PIN 13

#define START_CODE ((char)0xA9)
#define END_CODE   ((char)0x9A)

static byte lastValue = 0;
static byte currentValue = 0;
//static const char onCmd[] = { START_CODE, 0x17, 0x2E, 0x00, 0x00, 0x00, 0x3F, END_CODE };
//static const char offCmd[] = { START_CODE, 0x17, 0x2F, 0x00, 0x00, 0x00, 0x3F, END_CODE };
static const char onCmd[] = "UUUUUUUU";
static const char offCmd[] = "UUUUUUUU";

s_pin txPin = 15;
unsigned long timestamp = 0;

#define INTERBIT_26_US_SLEEP delayMicroseconds(41); NOPS(23)
#define SET_TX_PIN(level) digitalWrite(txPin, level); INTERBIT_26_US_SLEEP

static void txByte(char b, bool parity)
{
  noInterrupts();
  // start bit
  SET_TX_PIN(LOW);
  // 8 data bits
  SET_TX_PIN(b & (1 << 0) ? HIGH : LOW);
  SET_TX_PIN(b & (1 << 1) ? HIGH : LOW);
  SET_TX_PIN(b & (1 << 2) ? HIGH : LOW);
  SET_TX_PIN(b & (1 << 3) ? HIGH : LOW);
  SET_TX_PIN(b & (1 << 4) ? HIGH : LOW);
  SET_TX_PIN(b & (1 << 5) ? HIGH : LOW);
  SET_TX_PIN(b & (1 << 6) ? HIGH : LOW);
  SET_TX_PIN(b & (1 << 7) ? HIGH : LOW);
  // parity
  SET_TX_PIN(parity ? HIGH : LOW);
  // stop bit
  NOPS(40); // compensate a bit for the lack of comparison
  SET_TX_PIN(HIGH);
  // for perfect bit timing we should wait some more here,
  // but we don't.
  interrupts();
}

static bool CalculateParity(byte b)
{
  bool parity = false;
  while (b) {
    parity = !parity;
    b = b & (b - 1);
  }
  return b;
}

static void TurnOnProjector()
{
  Serial.print("Turning on projector... ");
  for (int i = 0; i < 8; ++i) {
    txByte(onCmd[i], CalculateParity(onCmd[i]));
  }
  digitalWrite (LED_PIN, HIGH);
  Serial.println("done.");
}

static void TurnOffProjector()
{
  Serial.print("Turning off projector... ");
  for (int i = 0; i < 8; ++i) {
    txByte(offCmd[i], CalculateParity(offCmd[i]));
  }
  digitalWrite(LED_PIN, LOW);
  Serial.println("done.");
}

// next macro sets up the Z-Uno channels
// in this example we set up 1 switch binary channel
// you can read more on http://z-uno.z-wave.me/Reference/ZUNO_SWITCH_BINARY/
ZUNO_SETUP_CHANNELS(ZUNO_SWITCH_BINARY(getter, setter));

// this function runs only once the controller sends new value
void setter (byte value) {
  currentValue = value;
}

// this function runs only once the controller asks
byte getter () {
  return currentValue;
}

void setup() {
  Serial.begin();
  pinMode(LED_PIN, OUTPUT);
  pinMode(txPin, OUTPUT);
  currentValue = lastValue = 0;
}

void loop() {
  if(millis() - timestamp > 3000) {
    Serial.print("PING - ");
    Serial.println(timestamp);
    timestamp = millis();
  }
  if (lastValue != currentValue) {
    lastValue = currentValue; 
    if (currentValue > 0) {
      TurnOnProjector();
    }
    else {
      TurnOffProjector();
    }
    delay(3000);
  }
}
The amount of delay between each bit was determined through trial and error until I got the timing right. Now, here's the problem: when I power the z-uno with my laptop, the timing is all wrong. The z-uno seems to be running at ~50 % compared to when I use a charger. Here are some measurements I did with my oscilloscope. The first picture is when the z-uno is connected to a USB power supply (5 V, 2.5 A) and the second is when it's connected to my laptop.

Image Image

I did not re-program the z-uno between these capture. I only changed the power supply.

What is wrong?

Re: Z-uno is running much slower on PC power than on charger

Posted: 17 Feb 2018 18:01
by PoltoS
Do you see this slow down effect when you power Z-Uno from a 5V USB charger. I want to exclude the possible influence from the PC sending some USB datagrams

Re: Z-uno is running much slower on PC power than on charger

Posted: 23 Feb 2018 02:10
by risca
I've done some more experiments on this now.
Things I have powered the z-uno with that makes it run slow:
  • my laptop (USB cable, 5 V, ? A)
  • old Siemens phone charger (5 V, 700 mA) soldered to +5 V and GND.
Things I have powered the z-uno with that makes it run fast:
  • USB charger (5 V, 2.5 A)
  • powered USB hub (5 V, 1.5 A)
I'm at a loss what could be causing this

Re: Z-uno is running much slower on PC power than on charger

Posted: 23 Feb 2018 03:23
by Tillsy
•my laptop (USB cable, 5 V, ? A)
A computer will only provide higher amperage out the USB once it has installed with a device specific driver.

•old Siemens phone charger (5 V, 700 mA) soldered to +5 V and GND.
So in both these cases you have identified low current when it runs slow, whereas in the cases it runs fast you are talking multiple amps.

Re: Z-uno is running much slower on PC power than on charger

Posted: 23 Feb 2018 22:33
by risca
Yes, it seems that way. The Z-uno seem to work fine in both cases, just 2 different speeds.

Re: Z-uno is running much slower on PC power than on charger

Posted: 27 Feb 2018 19:03
by PoltoS
Do you see same if powered on 3V pin?

Re: Z-uno is running much slower on PC power than on charger

Posted: 28 Feb 2018 04:48
by risca
I don't have any 3 V power supplies, or do you mean to connect 5 V to the 3 V pin?

Re: Z-uno is running much slower on PC power than on charger

Posted: 28 Feb 2018 13:34
by Tillsy
Oh hell don't do that! He meant feeding a 3V source to the 3V pin - the Z-UNO natively runs 3V probably why he wants to test that.

But that's not what you're needing/trying. 5V is convenient as any USB power source will run it - but in your case you're using a few USB sources that are limiting the current.

Re: Z-uno is running much slower on PC power than on charger

Posted: 02 Mar 2018 00:16
by PoltoS
NB! Don't connect 5 Volts to 3V pin!!!!

Z-Uno consumes about 50 mA without sensors connected, so your 200 mA power source should be enough.... I doubt how can this happen - UART and radio should not work neither if you see such a slow down. May be it is a faulty unit? Do you have a spare one?

Re: Z-uno is running much slower on PC power than on charger

Posted: 02 Mar 2018 16:49
by petergebruers
Long shot... Could it be some MCU pins pick up noise? That alone does not not explain what you see, the noise should also trigger something like an interrupt. Also I think all inputs default to weak pull-up, but to be on the safe side, could you set all unused pins to output mode?