0

So I need to automaically eject a CD from a Blu-Ray drive, I have a wire welded on the drive's card that if manually connected for a split second (pulse) to the ground of that same card on the drive, will eject the CD. Now in order to do that automatically I'm using an Arduino Nano and I connected that wire to an analog pin "d1" set its mode to Output and connected the grounds of both Arduino and drive together. Here's my code 1

int ds1 = A2; //pin that indicates the existence of the CD inside the drive if HIGH and its inexistance otherwise
int d1 = A3;  //pin connected to the wire that if connected to ground ejects the CD

void setup() {
  Serial.begin(9600);
  pinMode(ds1, INPUT);
  pinMode(d1, OUTPUT);
  while((!analogRead(ds1))) //if the CD isn't inside the drive wait
  {}
}

void loop() {
  analogWrite(d1,0); //eject CD
  delay(500);
  while(!analogRead(ds1)) //if the CD isn't inside the drive wait
  {}
  delay(1000);
}

I'm thinking that maybe setting the pin to 0 doesn't necessarily mean I connected it to the gnd, it's just a possibility if you see anything wrong with the code or have any suggestion please help me

5
  • majenko.co.uk/blog/what-exactly-gpio-pin
    – Majenko
    Aug 23 '18 at 11:04
  • pinMode(d1, OUTPUT); sets the pin LOW (0 zero) connected to ground
    – Juraj
    Aug 23 '18 at 11:18
  • 1
    while(!digitalRead(ds1)) would be better at first line in loop. then you can leave it out from setup()
    – Juraj
    Aug 23 '18 at 13:03
  • what do you mean by Blu-Ray drive? ..... is this a drive on a computer or is it a Blu-Ray player?
    – jsotola
    Aug 24 '18 at 4:58
  • a blu-ray player not connected to a computer
    – SalmaG
    Aug 24 '18 at 7:47
1

Yes, setting a pin to OUTPUT and setting it LOW effectively connects the pin to ground (via a few ohms of resistance).

There's a couple of things wrong with your code, though, which you should address:

  • Use digitalWrite() not analogWrite().

analogWrite(pin, 0) is effectively the same as digitalWrite(pin, 0), but it's ambiguous to someone reading your code.

  • You never "release" the pin, so once it goes LOW it stays LOW. You should emulate open drain by setting it to INPUT when not using it.

  • You should get into the habit of putting repeated code into functions to make it more understandable what's going on.

For example:

void setup() {
  Serial.begin(9600);
  pinMode(ds1, INPUT);
  pinMode(d1, INPUT);
  waitForCD();
}

void loop() {
  triggerEject();
  waitForCD();
  delay(1000);
}

void triggerEject() {
  pinMode(d1, OUTPUT);
  digitalWrite(d1, LOW); // Press the button
  delay(100); // This is how long the button is pressed for
  pinMode(d1, INPUT); // Release the button
}

void waitForCD() {
  while(!analogRead(ds1)) //if the CD isn't inside the drive wait
  {}
}
  • !analogRead(ds1) will only ever be true if the voltage at pin A2 is exactly zero volts. If you really want to read an analog voltage here you should find a threshold value that the value is compared to:

    while (analogRead(ds1) < 100)) {}

Or it may be that you actually want to use digitalRead()...

9
  • Setting a digital PIN to LOW doesn't set it to GND. It set it to 0. Just try for example with a HC12: to set it, you must link its SET PIN to GND. If you link the SET pin to real GND, it works. If you link it to D8 (eg) and set D8 to LOW, it doesn't work.
    – Peter
    Apr 23 at 13:15
  • @Peter I disagree entirely. When you set a pin to LOW it activates a low-side N-channel MOSFET with very low resistance to connect the pin to GND. Worst case would be 20Ω, but more likely <1Ω for the low-side. Which pales into insignificance compared to the 1kΩ resistor that the module slaps in there. Since it uses an STM8 MCU the input LOW threshold is 0.3VDD, which at 5V is 1.6V, which even with 20Ω plus 1kΩ against the 10kΩ pullup is a good 3-4x higher than the voltage the Arduino would give at that pin (0.46v). I suspect you just forgot to set the pin to OUTPUT mode.
    – Majenko
    Apr 23 at 13:29
  • Take a HC12 (eg) and read the DataSheet: if you put SET at GND, the HC12 will turn into setting mode. So try and you'll see it's true. Now, link to a PIN on the Arduino, set it to LOW. You'll see the HC 12 don't enter in setting mode (we've made also test with Ax PIN and result is the same. And "of course" we are in OUTPUt!) . Seems if you want to get a "real" GND you must trigger a NPN. Maybe using LOW it's "near to be GND" and so, in some case it will work.
    – Peter
    Apr 23 at 16:43
  • NPN will be even worse as you impose a 0.7v junction voltage drop on to of the resistive divider. I don't have a HC12, but please do some voltage measurements on the stm8 io pin and show me that its above 1.6v when pulled low by an Arduino.
    – Majenko
    Apr 23 at 16:46

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.