Wednesday, July 31, 2019

Using Resources to store images in a windows forms C# program


It is useful to store the images used by a program inside the executable as resources. Visual studio offers an efficient way to do that.

The method is very simple  but I always forget how to do it. So, this is a very short tutorial.

Note: I use Visual studio 2017 but the procedure is similar with other versions.

1.    Create the project

We use Visual studio to create a windows Forms App.


2.    Put the resource in the project

Open the Resource.resx file by double clicking on it. (Visual studio will open a specific editor)



In the toolbar of this editor select Add Resource and select “Add existing file” then choose the desired image file (here I will use summer.bmp).



This will create a new resource containing the desired image



3.    Use the image

You can now use the image resource in the designer i.e. as the image property of a Picture box



But you can also access the image in the code by using <program_name>.Properties.Resources.<resourcename>.

See example below:

namespace image_in_ressource
{
    public partial class Form1 : Form
    {
        private Image summerImage;
        public Form1()
        {
            InitializeComponent();
            summerImage = new Bitmap(image_in_ressource.Properties.Resources.summer);
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImageUnscaled(summerImage, new Point(0, 0));
        }
    }
}


Tuesday, May 14, 2019


Making a feeder for Märklin M Tracks


Even if Märklin M tracks are not manufactured anymore, it is very easy to find a lot of them online and since they are obsolete, they are often quite cheap.

But it is sometimes more problematic to find feeder tracks. So, let’s build our own.


We will use the very common 5106 straight M track.
We will solder the wires at the indicated places.




Before soldering the wires, we must use a small knife (I have used the blade a small flat screwdriver) to scratch the paint and the oxide.

To let the wires go out it is easy to drill a small hole on the metallic side of the track.


The metal is not hard at all to drill

Once everything is finished don’t forget to make a knot in the wires to prevent excessive constrains on the solders

And here it is, just as good as the original much harder to find 5111, ready to provide power to all your trains.


            



Thank you to my father who shown mehow to do that in 1979.

Sunday, June 10, 2018

Scrolling Text On LCD with Arduino

Introduction

We want to program the Arduino so that it will scroll (possibly very long) text acroos an LCD screen.
The text shall be sent to the Arduino via the serial port
The text transmitted by the PC via the USB link is stored in the Arduino Memory and displayed as a scrolling text on the LCD display.
The major risk is to lose data in case of very long text since the Arduino internal serial port buffer is limited to 64 bytes

The Hardware

The hardware is the classical Interfacing of an Arduino UNO with a 2x16 HD44780 LCD module. Note that for the module I did use did include a current limiting resistor in the circuit backlight. This may not be the case for your module. So be careful. You may destroy the module if you don’t include an external resistance on a module that has no internal limiting resistance.  (see https://electronics.stackexchange.com/questions/212197/is-it-necessary-to-use-resistor-when-connecting-backlight-of-16x2-lcd-display for details).

For the NPN transistor. I did use a BC547 but any similar transistor (BC108, 2N222,…) may be used instead.

The software

The software is made of 2 parts:

The display part

The display part builds a 16 characters string containing the part of the buffer to print (if needed padded with white spaces) and display it with a lcd.print command. This has the advantage of not disturbing the other lines of the display.
Since the Arduino serial port input buffer is very small (64 bytes), the display routine is immediately stopped once bytes are received in the serial port input buffer. (note: the process could probably be optimized using interrupts but tests did show the program was working correctly without using interrupts

The communication Part

This routine simply fills a large (256 bytes) buffer with the data received on the serial port using the method Serial.readBytesUntil('\n',buffer,256);
The handshaking is done via an XON XOFF mechanism.

Full listing

/*
 LCD Serial ScrollPrint

MIT License

Copyright (c) 2018 Pierre Poliakoff

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

 */

// include the library code:
#include <LiquidCrystal.h>

//declare the hardware
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

int pinBacklight=9;

//const
//=====
const byte XOFF=19;
const byte XON=17;
//variables
//=========
char buffer[256];



void LcdPrintAndScroll(char *buffer,int len)
{
int offset =-16;
String screen="                "; //16 chars buffer string
while(Serial.available()==0)
  {
  lcd.setCursor(0,0);
  offset++;
  if(offset>len)
   {
    offset=-16;
   }
 
  for (int i=0; i <16 && Serial.available()==0 ;i++)
    {
      if((i+offset<len)&&(i+offset>=0)&&buffer[i+offset]>31)
       {
        screen[i]=buffer[i+offset];
       }
      else
      {
        screen[i]=' ';
      }
    }
  lcd.print(screen);
  unsigned long now=millis();
  while(millis()-now<250 && Serial.available()==0)
    {
      //do nothing
    }
  }
}

void setup() {
  // initialize Hardware
  pinMode(pinBacklight,OUTPUT);
  digitalWrite(pinBacklight,HIGH);
  Serial.begin(9600);

  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);

  lcd.print("Waiting for Data");
  lcd.setCursor(0,1);
  lcd.print("Scroll Text Demonstration");

}
//===========================================
void loop() {
int len;
// receive data
while (Serial.available()==0)
  {
   //Do nothing 
  }
len=Serial.readBytesUntil('\n',buffer,256);
Serial.write(XOFF); // pause the transmission
Serial.flush();
// insert herre any long computation on Buffer
Serial.write(XON);//reactivate the transmission
//Print Data
if (len>0)
  {
    LcdPrintAndScroll(buffer,len);
  }

}

Sending Data from the PC

We do not want to write a specific software on the PC to send the data. We  simply use the Windows Command line features:
On my PC the Arduino is visible as COM4 (check on you PC for the exact value)
We configure the com port communication via the following command

mode COM4 BAUD=9600 xon=on data=8


Note that each time you upload a new firmware version in the Arduino, you must reconfigure the com port with the above command
Then you can send the data to display via a simple copy command

copy mydata.txt com4:

if you prefer to enter the data directly with the keyboard, you can use

copy con: com4:

Press enter to display the text on the Arduino and CTRL-Z to exit.




The Final Result




Sunday, April 1, 2018

TTL Logic families compatibility

Logic families compatibility

Introduction

A few year ago, the logic gates were all using 5V and everything was clear. But today (2018) we see more and more 3.3V logic. The Raspberry PI, the Arduino M0, the Digilent Basys3, the Wemos D1 Mini have all 3.3V inputs But the classical Arduino UNO has 5V logic ports.

This WEMOS D1 MINI PRO is has 3.3V ports but the Arduino MEGA ADK has 5V ports

This means that when you are designing a circuit, you have to take care of the logic level you are using and when needed use level shifters that will convert the logic signal to the correct level.

Logic families

There are quite a few digital circuits families. The most common are the 74HCxxx, the 74HCTxxx and the very old 74LSxxx but there are many others.





The input level determine what voltage range is acceptable for a HIGH level and what voltage level is acceptable for a LOW level

For a 74HCT00 input, the LOW level is associated to a voltage between 0V and 0.8V and a HIGH level is associated to a voltage between 2V and 5V.

This means that voltage below 0V or above 5 V may damage the chip and any voltage between 0.8V and 2V is not a well defined logic signal.


To know if the output of a gate is compatible with the 74HCT family we must know if the gate output level fall within the acceptable values. However, this is not as simple as one would expect as the gate output voltage is dependent on the gate power supply voltage (i.e. the VCC value) and the current that the gate provide (the more current is drawn from the gate the lower the HIGH voltage shall become).

If we take a 74ALVC00 with VCC = 2.7V and an output current of 12mA we have the HIGH voltage = 2.2V and a LOW output voltage of 0.4V.

We see that the the 74ALVC00 output voltage HIGH (2.2V in the selected conditions) fits inside the 74HCT00 High voltage input range (between 2 and 5V) and that the 74ALVC00 output voltage LOW (0.4V in the selected conditions) fits inside the 74HCT00 LOW voltage input range (between 0 and 0.8V)



So Yes a 74ALVC00 with VCC2.7V can drive a 74HCT00 wit VCC=5V


What happens if we do the opposite

The 74ALCV LOW input voltage ranges is between 0 and 0.7V and the HIGH input voltage is between 2V and 3.6V. The 74HCT has an output voltage LOW between 0 and 0.44V and an output voltage HIGH of at least 0.44V



This means that contrarily to the previous case, here the gates are not compatible and connecting them that way could damage the 74ALVC00 gate.




This incompatibility is due to the fact that the 74ALVC is not 5V tolerant. If we had used a 74LVC instead (which is a 5V tolerant family)  it would have been OK.

Compatibility table

The table below was obtained by analyzing the datasheets of a NAND gate (7400 or equivalent) from each logic family and comparing the input and output voltage ranges.



To use the table select an output family (e.g. 74HCT: last line) it will indicate to what kind of gate input it can be connected. In the 74HCT example, any family except the 74AC,74HC (if VCC is 3.3V) ,and 74 ALVC.

As you can see in the table there is much more green than red which is a very good news since this means that many families are indeed compatible


input
PSU =3V
PSU=5V
Logic type

CMOS in 3V
TTL3V ONLY
CMOS in 3V 5V Tolerant
TTL3V ONLY 5VTolerant
CMOS in 5V
CMOS in 5V
old CMOS
TTL5V only
CMOS 5V only compatible TTL

Family names
AC

HC
ALVC
AHC

LV

VHC
LCX

LVC

LVX
AC

HC
AHC

LV

VHC
CD4xxx ALS

AS

F

LS

S
ACT

AHCT

HCT
output PSU =3V CMOS in 3V
AC

HC
Y
Y
y
Y
N
N
N
Y
Y
TTL3V ONLY
ALVC
Y
Y
Y
y
N
N
N
Y
Y
CMOS in 3V 5V Tolerant
AHC

LV

VHC
y
Y
Y
Y
N
N
N
Y
Y
TTL3V ONLY 5VTolerant
LCX

LVC

LVX
Y
y
Y
Y
N
N
N
Y
Y
PSU=5V
CMOS in 5V
AC

HC
N
N
Y
Y
Y
y
Y
Y
Y
CMOS in 5V
AHC

LV

VHC
N
N
Y
Y
Y
y
Y
Y
Y
old CMOS
CD4xxx
N
N
Y
Y
Y
Y
Y
Y
Y
TTL5V only
ALS

AS

F

LS

S
N
N
Y
Y
N
N
N
Y
Y
CMOS 5V only compatible TTL
ACT

AHCT

HCT
N N Y Y Y Y Y Y Y

Adapting levels

As can be seen in the table above, there 3 types of incompatibilities

TTL 5V driving CMOS 5V

This is a very old incompatibility that dates back to 1970!

e.g. a 74LS driving a 74HC or a 74F driving a CD4000

The TTL 5V gates have an HIGH voltage level that is not high enough to be identified as a HIGH level by the CMOS gate

One simple solution is to interface both systems using a 74HCT gate. indeed the input of a 74HCT gate is compatible with TTL 5V and its output is compatible CMOS




TTL 3.3V driving CMOS 5V

This is the same incompatibility as for TTL 5V driving CMOS 5V: the TTL 3V output voltage HIGH is not HIGH enough. Here also, using a 74HCT is a good workaround.



5V Logic driving 3V3 Non 5V tolerant logic

This is the problem: that occurs when you want to drive a Raspberry PI input with a 5V signal. You risk to damage  the 3V3 raspberry PI chip with the 5V output. The workaround is to use 5V tolerant 3V3 logic gate.e.g. a 74LVC gate as interface.


DISCLAIMER:

 The compatibility table was made by analyzing the datasheet of one manufacturer (most of the times Texas Instruments and sometimes ON SEMI or other manufacturers) .Some extrapolation was needed when the datasheet did not contain the value of VCC (3.3V / 5V) or the gate output current (12mA). This means that the table is for reference only and I cannot guarantee it does not contain some errors or inaccuracies. This table is no replacement for consulting the manufacturer datasheet