Arduino PWM 5V to 10V converter

Discussion in 'D.I.Y.' started by DaChicken, Oct 10, 2014.

  1. DaChicken
    Offline

    DaChicken Green fingers

    • APSA Member
    51%
    Joined:
    Jun 8, 2013
    Messages:
    364
    Likes Received:
    123
    Trophy Points:
    53
    Location:
    Pretoria
    intensity = start_state + (per_second_delta)*elapsed.

    Start state = 0;

    per_second_delta = (end_state - start_state)/fade_duration

    end_state = 255;

    fade_duration = 30 minutes;
     
  2. f-fish+plants
    Offline

    f-fish+plants Aquascaper

    • APSA Member
    73%
    Joined:
    Jun 13, 2010
    Messages:
    1,922
    Likes Received:
    1,041
    Trophy Points:
    173
    Location:
    JHB - West
  3. f-fish+plants
    Offline

    f-fish+plants Aquascaper

    • APSA Member
    73%
    Joined:
    Jun 13, 2010
    Messages:
    1,922
    Likes Received:
    1,041
    Trophy Points:
    173
    Location:
    JHB - West
    I am assuming you have some thing like this .. in your code also, from
    http://arduino.cc/en/Tutorial/Dimmer

    Ignore the serial port chatter ..

    const int ledPin =9; // the pin that the LED is attached to


    voidloop(){
    byte brightness;

    // check if data has been sent from the computer:
    if(Serial.available()){
    // read the most recent byte (which will be from 0 to 255):
    brightness =Serial.read();
    // set the brightness of the LED:
    analogWrite(ledPin, brightness);
    }
    }
     
  4. DaChicken
    Offline

    DaChicken Green fingers

    • APSA Member
    51%
    Joined:
    Jun 8, 2013
    Messages:
    364
    Likes Received:
    123
    Trophy Points:
    53
    Location:
    Pretoria
    My dimming works 100% for a normal LED using 220 ohm resistor.

    Now I want to use the meanwell 60-48D driver... but my arduino only deliver almost 5V....

    and the meanwell driver requires 10V.

    So I am just looking for a way to convert the 0-5V to 0-10V...
     
  5. f-fish+plants
    Offline

    f-fish+plants Aquascaper

    • APSA Member
    73%
    Joined:
    Jun 13, 2010
    Messages:
    1,922
    Likes Received:
    1,041
    Trophy Points:
    173
    Location:
    JHB - West
    ah - ok, got it.
     
  6. f-fish+plants
    Offline

    f-fish+plants Aquascaper

    • APSA Member
    73%
    Joined:
    Jun 13, 2010
    Messages:
    1,922
    Likes Received:
    1,041
    Trophy Points:
    173
    Location:
    JHB - West
  7. DaChicken
    Offline

    DaChicken Green fingers

    • APSA Member
    51%
    Joined:
    Jun 8, 2013
    Messages:
    364
    Likes Received:
    123
    Trophy Points:
    53
    Location:
    Pretoria
    Thanx!

    I will give this a shot..... I have tried multiple transistor setups... I believe even this setup... but will try again... see if this helps....

    Will keep you updated on the progress....
     
  8. f-fish+plants
    Offline

    f-fish+plants Aquascaper

    • APSA Member
    73%
    Joined:
    Jun 13, 2010
    Messages:
    1,922
    Likes Received:
    1,041
    Trophy Points:
    173
    Location:
    JHB - West
    And any luck ?
     
  9. DaChicken
    Offline

    DaChicken Green fingers

    • APSA Member
    51%
    Joined:
    Jun 8, 2013
    Messages:
    364
    Likes Received:
    123
    Trophy Points:
    53
    Location:
    Pretoria
    Hey. Sorry, things are a bit hectic at work... Will try to test it this weekend... Will let you know asap...
     
  10. DaChicken
    Offline

    DaChicken Green fingers

    • APSA Member
    51%
    Joined:
    Jun 8, 2013
    Messages:
    364
    Likes Received:
    123
    Trophy Points:
    53
    Location:
    Pretoria
    Hey guys!!

    I had finally got some time to work on my LED unit....

    And yes...!!! It looks like it's working!!

    I have measured the voltage over each LED output and it measured 10.04V... and I am happy!!

    Thanx to all for the help you had given me!

    Hope this thread will help someone else here....

    Cheers!
     
  11. f-fish+plants
    Offline

    f-fish+plants Aquascaper

    • APSA Member
    73%
    Joined:
    Jun 13, 2010
    Messages:
    1,922
    Likes Received:
    1,041
    Trophy Points:
    173
    Location:
    JHB - West
    Good news ... what was the solution in the end?
     
  12. DaChicken
    Offline

    DaChicken Green fingers

    • APSA Member
    51%
    Joined:
    Jun 8, 2013
    Messages:
    364
    Likes Received:
    123
    Trophy Points:
    53
    Location:
    Pretoria
    The one you posted....

    I bought new Transistors.... the previous transistors was faulty.....

    Thanx again!!
     
  13. Wazi
    Offline

    Wazi Green fingers

    • APSA Member
    9%
    Joined:
    Oct 12, 2014
    Messages:
    201
    Likes Received:
    93
    Trophy Points:
    38
    Location:
    Alberton
    Hey DaChicken, im busy working on a similar project but i want to include a lcd display and a temp probe. Would it be possible for you to pm me your arduino sketch?
     
  14. DaChicken
    Offline

    DaChicken Green fingers

    • APSA Member
    51%
    Joined:
    Jun 8, 2013
    Messages:
    364
    Likes Received:
    123
    Trophy Points:
    53
    Location:
    Pretoria
    Hey man.... I have just tested the light unit and it was suppose to switch off... But it didn't....

    So it doesn't work... I will have to manually switch on and off the lights... Until that problem is solved....

    Hey Wade.. If you wait a while I have a bigger project for you...

    Currently I am waiting for my dosing pumps... Once it arrives... My project will do the following :
    Sunrise and sunset
    Auto dosing 3 different ferts
    Measure temp
    Measure pH
    Lcd user interface with keyboard

    I only need to install the dosing pumps and get this light problem solved...

    It is an Arduino shield which I have designed...

    Where do you live? Maybe we can get together and discuss this...
     
  15. DaChicken
    Offline

    DaChicken Green fingers

    • APSA Member
    51%
    Joined:
    Jun 8, 2013
    Messages:
    364
    Likes Received:
    123
    Trophy Points:
    53
    Location:
    Pretoria
    Ha ha... Sorry Waze... Auto correct on phone... Lol
     
  16. Wazi
    Offline

    Wazi Green fingers

    • APSA Member
    9%
    Joined:
    Oct 12, 2014
    Messages:
    201
    Likes Received:
    93
    Trophy Points:
    38
    Location:
    Alberton
    Haha. No probs. I stay in Alberton, good old powerless jhb. Lol. Im by no means an expert in electronics but do know my way around a pcb and coding. Sounds like its going to be a mean project. Cant wait to see the final project.
     
  17. f-fish+plants
    Offline

    f-fish+plants Aquascaper

    • APSA Member
    73%
    Joined:
    Jun 13, 2010
    Messages:
    1,922
    Likes Received:
    1,041
    Trophy Points:
    173
    Location:
    JHB - West

    NO da Chicken's but this sketch has been running since 07 2012 on an Uno of mine.

    Code:
    
    /*-----( Import needed libraries )-----*/
    #include <SD.h>
    #include <Dht11.h>
    #include <Wire.h>
    #include <LiquidCrystal_I2C.h>
    #include <OneWire.h>
    #include <RTClib.h>
    #include <DallasTemperature.h>
    #include <PString.h>
    
    RTC_DS1307 RTC;
    
    OneWire oneWire(2);
    DallasTemperature sensors(&oneWire);
    DeviceAddress Temp1 ;
    int NOTS = 0;
    int NOTSC = 0;
    
    char bufferSDate[13];
    PString strSDate(bufferSDate, sizeof(bufferSDate));
    char bufferNDate[40];
    PString strNDate(bufferNDate, sizeof(bufferNDate));
    char bufferDate[40];
    PString strDate(bufferDate, sizeof(bufferDate));
    char bufferTop[40];
    PString strTop(bufferTop, sizeof(bufferTop));
    char bufferBot[40];
    PString strBot(bufferBot, sizeof(bufferBot));
    
    LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27
    dht11 DHT11;
    #define DHT11PIN 6
    
    void setup()   /*----( SETUP: RUNS ONCE )----*/
    {
      Wire.begin();
      RTC.begin();
      // following line sets the RTC to the date & time this sketch was compiled
      //RTC.adjust(DateTime(__DATE__, __TIME__));
    
      lcd.init();
      lcd.backlight();
    
      pinMode(10, OUTPUT); // SD Card CS pin
      SD.begin(10);
      delay(100);
    
      sensors.begin();
      NOTSC = sensors.getDeviceCount();
      for (NOTS = 0; NOTS < NOTSC; NOTS++){
        if (!sensors.getAddress(Temp1, NOTS)) //Serial.println("Unable to find address for Device 1");
          sensors.setResolution(Temp1, 11);
    
      }
    }
    
    void loop()
    {
      setMDate();
      getData();
      //readLog(); 
      //print to LCD
      lcd.clear();
      lcd.print(strTop);
      lcd.setCursor(0,1);
      lcd.print(strBot);
    
      delay(300000);
    }
    
    void readLog()
    {
      Serial.begin(57600);
      File myFile = SD.open(strSDate);
      while (myFile.available()) {
        Serial.write(myFile.read());
      }
      myFile.close();
    
    }
    
    void setMDate()
    {
      DateTime now = RTC.now();
    
      // Set up filename
      strSDate=now.year();
      strSDate += now.month();
      strSDate += now.day();
      strSDate += ".txt";
    
      // Setup log Date and time
      strDate = now.year();
      strDate += "/";
      if(now.month() <10) strDate +="0";
      strDate += now.month();
      strDate += "/";
      if(now.day() <10) strDate +="0";
      strDate += now.day();
      strDate += " ";
      if(now.hour() <10) strDate +="0";
      strDate += now.hour();
      strDate += ":";
      if(now.minute() <10) strDate +="0";
      strDate += now.minute();
      strDate += ":";
      if(now.second() <10) strDate +="0";
      strDate += now.second();
    
      //Setup LCD time display
      strNDate = "";
      if(now.hour() <10) strNDate +="0";
      strNDate += now.hour();
      strNDate += ":";
      if(now.minute() <10) strNDate +="0";
      strNDate += now.minute();
      strNDate += ":";
      if(now.second() <10) strNDate +="0";
      strNDate += now.second();
    }
    
    void getData()
    {
      File myFile = SD.open(strSDate, FILE_WRITE);
      // operate on TH sensor
      DHT11.read(DHT11PIN);
      sensors.requestTemperatures();
    
      strTop = strNDate;
      strTop += " C=";
      strTop += DHT11.temperature;
      strTop += " H=";
      strTop += DHT11.humidity;
    
    
      myFile.print(strDate);
      myFile.print(F(" Temp DHT11 "));
      myFile.println(DHT11.temperature);
    
      myFile.print(strDate);
      myFile.print(F(" Humi DHT11 "));
      myFile.println(DHT11.humidity);
    
    
      //operate on DS probes
    
      strBot = "";
      for (NOTS = 0; NOTS < NOTSC; NOTS++){
        sensors.getAddress(Temp1, NOTS);
    
        myFile.print(strDate);
        myFile.print(F(" Temp "));
        for (uint8_t i = 0; i < 8; i++)
        {
          //lcd.print(Temp1[i]);
          myFile.print(Temp1[i]);
        }
        myFile.print(" ");
        myFile.println(sensors.getTempCByIndex(NOTS));
    
        strBot += NOTS;
        strBot += "=";
        strBot += sensors.getTempCByIndex(NOTS);
        strBot += " ";
      }
      myFile.close();
    }
    Project almost detailed at http://www.tropicalaquarium.co.za/showthread.php?12650-Arduino-and-your-tank&highlight=arduino

    Just got a Yun - so want to upgrade the entire thing.

    Later Ferdie
     
  18. DaChicken
    Offline

    DaChicken Green fingers

    • APSA Member
    51%
    Joined:
    Jun 8, 2013
    Messages:
    364
    Likes Received:
    123
    Trophy Points:
    53
    Location:
    Pretoria
    Ha ha!!

    Thanx Ferdie.

    Yes, I would first like to finish my project before I share it. I want it to be open to APSA members due to the fact that we help each other out.
    I also added an alarm to the system which will detect water leakage if a bottom glass break as well as an alarm which notifies me when my filter is dirty....

    Here is the code only for the 3 LED strips:

    Code:
    #include <TimerOne.h>
    #include <Time.h>
    #include <Wire.h>
    #include <DS1307RTC.h>
    #include <OneWire.h> // added to original code by O2Surplus 4/2/2014
    #include <LiquidCrystal.h>// added to original code by O2Surplus 4/2/2014
    
    LiquidCrystal lcd(7,8,3,5,6,4);  // Set the LCD I2C address //LiquidCrystal lcd(0x27,20,4);  // set the LCD address to 0x27 for a 20 chars and 4 line display
    
    int DS18S20_Pin = 2; //DS18S20 Signal pin on digital 2 //Temperature chip i/o
    OneWire ds(DS18S20_Pin);  // on digital pin 2
    
    int fanEnable = 13;// assigns cooling fan ON/OFF control to digital pin 8.
    
    const int LED[3] = {10, 9, 11}; // Red, yellow, blue
    const long ledTimeTurnOn[3] = {48600, 48630, 79200}; //Seconds after midnight
    const long ledTimeTurnOff[3] = {72030, 72000, 84000}; //Seconds after midnight
    const int ledOnIntensity[3] = {255, 255, 100};
    const int ledOffIntensity[3] = {0, 0 , 0};
    const long ledFadeDuration[3] = {1800, 1800, 1800}; //seconds
    int setupLights[3] = {0, 0, 0};
    
    long currentTimeInSeconds, aWhileAgoInSeconds = 0;
    int lcdModeWritten[] = {0, 0, 0, 0, 0};
    
    int fader(long start_time, const int start_state, const int end_state, int ledFadeDuration)
    {
      int intensity;
      float per_second_delta;
      long elapsed = currentTimeInSeconds - start_time;
     
      per_second_delta = (float) (end_state - start_state) / ledFadeDuration;
      return intensity = start_state + (per_second_delta * elapsed);
    }
    
    long seconds_since_midnight()
    {
      time_t t = now();
      long hr = hour(t);
      long min = minute(t);
      long sec = second(t);
      long total = hr * 3600 + min * 60 + sec;
      return total;
    }
    
    void set_state(int state, int ledNumber)
    {
      if ((state >= ledOffIntensity[ledNumber]) && (state <= ledOnIntensity[ledNumber])) analogWrite(LED[ledNumber], state);
    }
    void writeModeToLcd(int mode)
    {
      boolean allReadyWrittenToLcd = false;
      for(int x = 1; x <= 5; x++)
      {
        if ((x == mode) && (lcdModeWritten[x] == 1)) allReadyWrittenToLcd = true;
      }
     
      if (allReadyWrittenToLcd == false)
      {
        lcd.setCursor(0,2);
       
        switch (mode)
        {
          case 1: lcd.print("     NIGHT/MODE     ");
          break;
          case 2: lcd.print("    SUNRISE/MODE    ");
          break;
          case 3: lcd.print("     MOON/MODE      ");
          break;
          case 4: lcd.print("      DAY/MODE      ");
          break;
          case 5: lcd.print("    SUNSET/MODE     ");
          break;
        }
       
        for(int x = 1; x <= 5; x++)
        {
          if (mode != x) lcdModeWritten[x] = 0;
          else lcdModeWritten[x] = 1;
        }
      }
    }
    
    void writeModeToLcdFunction()
    {
      if (currentTimeInSeconds < ledTimeTurnOn[0]) writeModeToLcd(1);
      else if (currentTimeInSeconds < ledTimeTurnOn[1] + ledFadeDuration[1]) writeModeToLcd(2);
      else if (currentTimeInSeconds < ledTimeTurnOff[1]) writeModeToLcd(4);
      else if (currentTimeInSeconds < ledTimeTurnOff[0] + ledFadeDuration[0]) writeModeToLcd(5);
      else if (currentTimeInSeconds < ledTimeTurnOn[2]) writeModeToLcd(1);
      else if (currentTimeInSeconds < ledTimeTurnOff[2]) writeModeToLcd(3);
      else writeModeToLcd(1);
    }
    
    void determineLedStates()
    {
      for (int x = 0; x <= 2; x++)
      {
        if ((currentTimeInSeconds >= ledTimeTurnOn[x]) && (currentTimeInSeconds <= (ledTimeTurnOn[x] + ledFadeDuration[x])))
        {
          int intensity = fader(ledTimeTurnOn[x], ledOffIntensity[x], ledOnIntensity[x], ledFadeDuration[x]);
          set_state(intensity, x);
        }
        else if ((currentTimeInSeconds >= ledTimeTurnOff[x]) && (currentTimeInSeconds <= ledTimeTurnOff[x] + ledFadeDuration[x]))
        {
          int intensity = fader(ledTimeTurnOff[x], ledOnIntensity[x], ledOffIntensity[x], ledFadeDuration[x]);
          set_state(intensity, x);
        }
        else if ((currentTimeInSeconds > ledTimeTurnOn[x] + ledFadeDuration[x]) && (currentTimeInSeconds < ledTimeTurnOff[x]) && (setupLights[x] == 0))
        {
          int intensity = ledOnIntensity[x];
          set_state(intensity, x);
          setupLights[x] = 1;
        }
      }
    }
    
    void printDigits(int digits)
    {
      Serial.print(":");
      if (digits < 10) Serial.print('0');
      Serial.print(digits);
    }
    
    void displayDigits(int digits)
    {
      lcd.print(":");
      if (digits < 10) lcd.print('0');
      lcd.print(digits);
    }
    
    void digitalClockDisplay()
    {
      Serial.print("ctr: ");
      Serial.print(currentTimeInSeconds); // display counter
      Serial.println();
      Serial.print(hour());
      printDigits(minute());
      printDigits(second());
      Serial.print(" ");
      Serial.print(month());
      Serial.print("/");
      Serial.print(day());
      Serial.print("/");
      Serial.print(year());
      Serial.println();
    }
    
    void lcdClockDisplay()
    {
      lcd.setCursor(0,0);
      lcd.print(hour());
      displayDigits(minute());
      displayDigits(second());
      lcd.print("  ");
      lcd.print(month());
      lcd.print("/");
      lcd.print(day());
      lcd.print("/");
      lcd.print(year());
      digitalClockDisplay();
    }
    
    float getTemp()
    {
      byte data[12];
      byte addr[8];
     
      if (!ds.search(addr))
      {
        ds.reset_search();
        return -1000;
      }
      if (OneWire::crc8(addr, 7) != addr[7])
      {
        Serial.println("CRC is not valid!");
        return -1000;
      }
      if ( addr[0] != 0x10 && addr[0] != 0x28)
      {
        Serial.print("Device is not recognized");
        return -1000;
      }
    
      ds.reset();
      ds.select(addr);
      ds.write(0x44,1); // start conversion, with parasite power on at the end
      byte present = ds.reset();
      ds.select(addr);   
      ds.write(0xBE); // Read Scratchpad
      for (int i = 0; i < 9; i++)
      { // we need 9 bytes
        data[i] = ds.read();
      }
      ds.reset_search();
      byte MSB = data[1];
      byte LSB = data[0];
      float tempRead = ((MSB << 8) | LSB); //using two's compliment
      float TemperatureSum = tempRead / 16;
     
      return TemperatureSum;
    }
    
    void lcdTemperatureDisplay()
    {
      float temperature = getTemp();// Temperature serial display coding addded by O2Suplus 4/2/2014
      Serial.println(temperature);
      lcd.setCursor(0,3);          // Temperature to I2C LCD added by O2Surplus 4/2/2014
      lcd.print("Temp C = ");
      lcd.print(temperature);
    }
    
    void setup()
    {
      Serial.begin(9600); // Max for Arduino Uno
      setSyncProvider(RTC.get);
      setSyncInterval(120);
      Wire.begin();
      lcd.begin(20,4);         // initialize the lcd for 20 chars 4 lines and turn on backlight
      //Timer1.initialize(6666); // 150Hz PWM
      pinMode(LED[0], OUTPUT);    
      //Timer1.pwm(LED[0], 0);
      pinMode(LED[1], OUTPUT);    
      //Timer1.pwm(LED[1], 0);
      //pinMode(fanEnable,OUTPUT);   // Fan control coding added by O2Surplus 4/2/2014
      pinMode(LED[2], OUTPUT);    
    }
    
    void loop ()
    {
      long previousTimeInSeconds = currentTimeInSeconds;
      currentTimeInSeconds = seconds_since_midnight();
     
      if (previousTimeInSeconds != currentTimeInSeconds)
      {
        writeModeToLcdFunction();
        determineLedStates();
        lcdClockDisplay();
      }
     
      if (aWhileAgoInSeconds <= currentTimeInSeconds - 10)
      {
        lcdTemperatureDisplay();
        aWhileAgoInSeconds = currentTimeInSeconds;
      }
    }
    I also started out as a newbie.... The first time i ever touched an arduino board was middle July 2014.... since then I started experimenting....
    but thanx to the internet, there is already a lot of code written... it is just a matter of cut and paste and improvise....

    Will keep you updated when it is done!
     
  19. DaChicken
    Offline

    DaChicken Green fingers

    • APSA Member
    51%
    Joined:
    Jun 8, 2013
    Messages:
    364
    Likes Received:
    123
    Trophy Points:
    53
    Location:
    Pretoria
    I still can't understand the problem I encountered last night with the LED unit....

    When disconnect the pins of the Arduino to the LED drivers.... the LEDs switch off completely... and when I connect it again... it goes on.... which is great....

    But if I try to set the voltage at the pin to 0, it still stays on... and when I set it to 255 it still stays on.... meaning that the Arduino doesn't control the LEDs.... somewhere it is bypassing the Arduino but do not know how????

    Anybody having the same problem/ experienced the same problem?

    Ferdie, how are you converting the 0-5V on your system to 0-10V?
     
  20. f-fish+plants
    Offline

    f-fish+plants Aquascaper

    • APSA Member
    73%
    Joined:
    Jun 13, 2010
    Messages:
    1,922
    Likes Received:
    1,041
    Trophy Points:
    173
    Location:
    JHB - West
    Not running lights on my system ... prefer the mechanical timer, that said I have invested in a relay shield .. might use that at some point.

    BTW sounds like you have floating pins .. on your output ..
     

    Attached Files:

Share This Page